Home     About     Download     Resources     Contact us

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

Browse thread
is there a switch or cond-statement? + type-problem
• circ ular
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: -- (:) From: circ ular Subject: is there a switch or cond-statement? + type-problem
how can I do like this or use cond or case?
def power(nbr, po):
if po > 0:
return nbr * power(nbr, po-1)
if po < 0:
return 1 / power(nbr, -1 * po)
if po == 0:
return 1

and ofc make it tailrecursive but that I know how.

also, how do I get it to be a float-function?

let rec powerx(n, pow, acc) =
if pow > 0.0
then powerx(n, pow -. 1, acc *. n)
else if pow < 0.0
then 1.0 /. powerx(n, pow *. (-1), acc)
else acc ;;

Characters 70-71:
then powerx(n, pow -. 1, acc *. n)
^
This expression has type int but is here used with type float
#

but it is used with float evrywhere no?