functional programming - OCaml : Simple function with conditionals doesn't work -


i new ocaml , in process of learning it. trying execute simple function calculates (a+b)^2 or (a-b)^2 based on values of a , b

i trying have function below

let a_squared_b b =  if(a<0 || b<0)  (a**2 + b**2 + 2*a*b)   else  (a**2 + b**2 - 2*a*b);; 

which returns warning

error: expression has type int  expression expected of type float 

so tried 1 below :

let a_squared_b (a:float) (b:float) : float =  if(a<0 || b<0)  (a**2 + b**2 + 2*a*b)  else (a**2 + b**2 - 2*a*b);; 

which warns something. hence proceeded forward check if function @ least works returns wrong result -

a_squared_b 2 2;; - : int = 0          

i not sure doing wrong, appreciated

in short, ocaml uses different operators integers , floats, i.e. ( *. ) instead ( * ), (+.) instead (+), etc. should use 2. instead of 2 "variable" of float type.

# let a_squared_b (a:float) (b:float) : float = if(a<0. || b<0.) (a**2. +. b**2. +. 2. *. a*. b) else (a**2. +. b**2. -. 2. *. a*. b);; val a_squared_b : float -> float -> float = <fun>
# a_squared_b 2. 2.;;

more information can get, example, there


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -