caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* is there a switch or cond-statement? + type-problem
@ 2008-09-02  3:23 circ ular
  2008-09-02  4:02 ` Chris Conway
  0 siblings, 1 reply; 2+ messages in thread
From: circ ular @ 2008-09-02  3:23 UTC (permalink / raw)
  To: caml-list

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?


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: is there a switch or cond-statement? + type-problem
  2008-09-02  3:23 is there a switch or cond-statement? + type-problem circ ular
@ 2008-09-02  4:02 ` Chris Conway
  0 siblings, 0 replies; 2+ messages in thread
From: Chris Conway @ 2008-09-02  4:02 UTC (permalink / raw)
  To: caml-list

circ ular <circularfunc <at> gmail.com> writes:

> 
> 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.

The closest thing to "cond" or "case" in OCaml is "match", but "if-then-else" is
probably clearer. In any case, here's how you could do it with "match":

let rec powerx(n, pow, acc) =
    match pow with
      | 0.0 -> acc
      | x when x < 0.0 -> 1.0 /. powerx(n, pow *. (-1.0), acc)
      | _ ->  powerx(n, pow -. 1.0, acc *. n);;

The "x" in the second case matches any floating point number, but the "when"
clause restricts it to negatives. The "_" in the last case therefore matches
only positive numbers.

> 
> 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?


"1" is a int constant. Use "1." or "1.0" for floats.

Regards,
Chris

> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
> 
> 





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-09-02  4:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-09-02  3:23 is there a switch or cond-statement? + type-problem circ ular
2008-09-02  4:02 ` Chris Conway

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).