From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id E8F0ABBAF for ; Tue, 2 Sep 2008 06:03:01 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AoUBACVYvEhQW+UCgWdsb2JhbACSSgEBECAEmg2Eej2BaQ X-IronPort-AV: E=Sophos;i="4.32,312,1217800800"; d="scan'208";a="16702375" Received: from discorde.inria.fr ([192.93.2.38]) by mail1-smtp-roc.national.inria.fr with ESMTP; 02 Sep 2008 06:03:01 +0200 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id m82431cV020577 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Tue, 2 Sep 2008 06:03:01 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AoUBACVYvEhQW+UCgWdsb2JhbACSSgEBECAEmg2Eej2BaQ X-IronPort-AV: E=Sophos;i="4.32,312,1217800800"; d="scan'208";a="16702374" Received: from main.gmane.org (HELO ciao.gmane.org) ([80.91.229.2]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/AES256-SHA; 02 Sep 2008 06:03:01 +0200 Received: from list by ciao.gmane.org with local (Exim 4.43) id 1KaN6W-0002Bo-S7 for caml-list@inria.fr; Tue, 02 Sep 2008 04:02:57 +0000 Received: from cpe-69-203-209-238.nyc.res.rr.com ([69.203.209.238]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 02 Sep 2008 04:02:56 +0000 Received: from cconway by cpe-69-203-209-238.nyc.res.rr.com with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 02 Sep 2008 04:02:56 +0000 X-Injected-Via-Gmane: http://gmane.org/ To: caml-list@inria.fr From: Chris Conway Subject: Re: is there a switch or cond-statement? + type-problem Date: Tue, 2 Sep 2008 04:02:47 +0000 (UTC) Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: main.gmane.org User-Agent: Loom/3.14 (http://gmane.org/) X-Loom-IP: 69.203.209.238 (Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008072820 Firefox/3.0.1) Sender: news X-Miltered: at discorde with ID 48BCBAF5.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; def:01 ocaml:01 beginner's:01 ocaml:01 bug:01 1.0:98 1.0:98 beginners:01 rec:01 rec:01 clearer:01 caml-list:01 caml-list:01 int:01 int:01 circ ular 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 > >