caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* RE: [Caml-list] function type confusion (using let)
@ 2001-09-03  6:26 FALCON Gilles FTRD/DTL/LAN
  0 siblings, 0 replies; 7+ messages in thread
From: FALCON Gilles FTRD/DTL/LAN @ 2001-09-03  6:26 UTC (permalink / raw)
  To: 'Laurent Chéno', Michael Leary; +Cc: Caml-list



-----Message d'origine-----
De : Laurent Chéno [mailto:laurent.cheno@noos.fr]
Envoyé : lundi 3 septembre 2001 07:55
À : Michael Leary
Cc : Caml-list
Objet : Re: [Caml-list] function type confusion (using let)


(* please excuse my poor english *)

> I was trying to be clever about not using ;; and minimizing my use of ; 
> in
> files, and I've gone off the deep end...
>
> Why is this wrong, and how do I fix it?:
>
> 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
                      ^ this "then" has no else !

>     if x < -.pi then  (* <------ here *)
>       pi2 +. x
>     else
>       x

... else ??

You have forgotten the "else" clause : if <cond> then <expr>   has 
type : unit
if <cond> then <float-expr> else <float-expr> has type : float
-------------------
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
-------------------
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


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

* Re: [Caml-list] function type confusion (using let)
  2001-09-03  5:51 ` Michael Leary
@ 2001-09-04  4:51   ` ITO Tsuyoshi
  0 siblings, 0 replies; 7+ messages in thread
From: ITO Tsuyoshi @ 2001-09-04  4:51 UTC (permalink / raw)
  To: caml-list

From: Michael Leary <leary@nwlink.com>
Subject: Re: [Caml-list] function type confusion (using let)
Date: Sun, 2 Sep 2001 22:51:40 -0700
Message-ID: <20010902225140.A22597@ip178.usw22.rb1.bel.nwlink.com>

> Well, this compiles, but looks funny:
> 
> let normr r =
>   let x = mod_float r pi2 in
>   match x > 0.0 with
>   | true ->
>       if x > pi then
>         -.pi2 +. x
>       else
>         x
>   | false ->
>       if x < -.pi then
>         pi2 +. x
>       else
>         x
> 
> Is there a different way?

I'm not sure if I got what you mean correctly, but I think you can
write:
    let normr r =
      let x = mod_float r pi2 in
      if x > 0.0 then begin
        if x > pi then
          -.pi2 +. x
        else
          x
      end
      else begin
        if x < -.pi then
          pi2 +. x
        else
          x
      end

(Two begin-end pairs are not necessary in the code above but they are
 for readability; in case you are confused by them, just ignore them.)

Of course you can write it in a more straightforward way if you don't
care the order of the tests....
    let normr r =
      let x = mod_float r pi2 in
      if x > pi then
        -.pi2 +. x
      else if x >= -.pi then
        x
      else
        pi2 +. x

-- ITO Tsuyoshi  <tsuyoshi@is.s.u-tokyo.ac.jp> --
-------------------
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


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

* Re: [Caml-list] function type confusion (using let)
  2001-09-03  5:25 Michael Leary
  2001-09-03  5:46 ` Patrick M Doane
  2001-09-03  5:51 ` Michael Leary
@ 2001-09-03  5:55 ` Laurent Chéno
  2 siblings, 0 replies; 7+ messages in thread
From: Laurent Chéno @ 2001-09-03  5:55 UTC (permalink / raw)
  To: Michael Leary; +Cc: Caml-list

(* please excuse my poor english *)

> I was trying to be clever about not using ;; and minimizing my use of ; 
> in
> files, and I've gone off the deep end...
>
> Why is this wrong, and how do I fix it?:
>
> 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
                      ^ this "then" has no else !

>     if x < -.pi then  (* <------ here *)
>       pi2 +. x
>     else
>       x

... else ??

You have forgotten the "else" clause : if <cond> then <expr>   has 
type : unit
if <cond> then <float-expr> else <float-expr> has type : float
-------------------
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


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

* Re: [Caml-list] function type confusion (using let)
  2001-09-03  5:46 ` Patrick M Doane
@ 2001-09-03  5:53   ` Michael Leary
  0 siblings, 0 replies; 7+ messages in thread
From: Michael Leary @ 2001-09-03  5:53 UTC (permalink / raw)
  To: Patrick M Doane; +Cc: caml

oops.  thanks.  I've been staring at this too long, I think.

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


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

* Re: [Caml-list] function type confusion (using let)
  2001-09-03  5:25 Michael Leary
  2001-09-03  5:46 ` Patrick M Doane
@ 2001-09-03  5:51 ` Michael Leary
  2001-09-04  4:51   ` ITO Tsuyoshi
  2001-09-03  5:55 ` Laurent Chéno
  2 siblings, 1 reply; 7+ messages in thread
From: Michael Leary @ 2001-09-03  5:51 UTC (permalink / raw)
  To: caml

I always think of something right after I ask... which is why I ask, I
suppose...

Well, this compiles, but looks funny:

let normr r =
  let x = mod_float r pi2 in
  match x > 0.0 with
  | true ->
      if x > pi then
        -.pi2 +. x
      else
        x
  | false ->
      if x < -.pi then
        pi2 +. x
      else
        x

Is there a different way?

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


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

* Re: [Caml-list] function type confusion (using let)
  2001-09-03  5:25 Michael Leary
@ 2001-09-03  5:46 ` Patrick M Doane
  2001-09-03  5:53   ` Michael Leary
  2001-09-03  5:51 ` Michael Leary
  2001-09-03  5:55 ` Laurent Chéno
  2 siblings, 1 reply; 7+ messages in thread
From: Patrick M Doane @ 2001-09-03  5:46 UTC (permalink / raw)
  To: Michael Leary; +Cc: caml

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


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

* [Caml-list] function type confusion (using let)
@ 2001-09-03  5:25 Michael Leary
  2001-09-03  5:46 ` Patrick M Doane
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Michael Leary @ 2001-09-03  5:25 UTC (permalink / raw)
  To: caml

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

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

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

File "fbot.ml", line 118, characters 4-52:
This expression has type float but is here used with type unit

I want normr to be float -> float


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


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

end of thread, other threads:[~2001-09-04  4:51 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-09-03  6:26 [Caml-list] function type confusion (using let) FALCON Gilles FTRD/DTL/LAN
  -- strict thread matches above, loose matches on Subject: below --
2001-09-03  5:25 Michael Leary
2001-09-03  5:46 ` Patrick M Doane
2001-09-03  5:53   ` Michael Leary
2001-09-03  5:51 ` Michael Leary
2001-09-04  4:51   ` ITO Tsuyoshi
2001-09-03  5:55 ` Laurent Chéno

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