Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] function type confusion (using let)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Patrick M Doane <patrick@w...>
Subject: Re: [Caml-list] function type confusion (using let)
On Sun, 2 Sep 2001, Michael Leary wrote:

> I was trying to be clever about not using ;; and minimizing my use of ; in
> files, and I've gone off the deep end...

Nothing to do with the use of ; here...

> Why is this wrong, and how do I fix it?:

Build up expressions slowly until you have a good understanding of how the
typechecker works.  The conditional statement

  if e1 then e2

has type unit and requires e1 to have type bool and e2 to have type unit

The conditional expression

  if e1 then e2 else e3

has type 'a where the types of e2 and e3 are both 'a. Similarly e1 has
type bool.

In your code, the last if clause is missing an else branch so there is a
type error.  What do you want the result to be if x is not > 0.0 and not <
0.0?

> let pi = 3.14159
> let pi2 = 2.0 *. pi
> 
> let normr r =
>   let x = mod_float r pi2 in
>   if x > 0.0 then
>     if x > pi then
>       -.pi2 +. x
>     else
>       x
>   else if x < 0.0 then
>     if x < -.pi then  (* <------ here *)
>       pi2 +. x
>     else
>       x

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr