List Info

Thread: "ocaml_beginners"::[] sign_int (example from manual)




"ocaml_beginners"::[] sign_int (example from manual)
user name
2006-11-23 07:52:51

In the following example:
#type sign = Positive | Negative;;
type sign = Positive | Negative
# let sign_int n = if n >= 0 then Positive else Negative;;
val sign_int : int -> sign = <fun>;
# sign_int ( 0 - 5 );;
- : sign = Negative
# sign_int 5;;
- : sign = Positive
# let a = 0 - 5;;
val a : int = -5
# sign_int a;;
- : sign = Negative
# sign_int -5;;
This expression has type int -> sign but is here used with type int

Why the error in the last line?

Regards
Johann
--
Johann Spies Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

";The LORD is nigh unto all them that call upon him, to
all that call upon him in truth.&quot;
Psalms 145:18

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] sign_int (example from manual)
user name
2006-11-23 14:45:42

Johann Spies a écrit :
> # sign_int -5;;
> This expression has type int -> sign but is here used with type int
>
> Why the error in the last line?
>;
>
Because sign_int has type int -> sign.
But before the '-', OCaml expects an int.

try this:

# sign_int (-5);;

Salutations

Matt

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] sign_int (example from manual)
user name
2006-11-23 15:16:17

I stumbled upon that same problem when I first began
looking at OCaml and discovered the same solution. But
still, it seems weird that -5 is not an int without
wrapping it in parens.

Michael

--- Matthieu Dubuget < matthieu.dubuget%40laposte.net">matthieu.dubugetlaposte.net>
wrote:

>; Johann Spies a écrit :
> > # sign_int -5;;
> > This expression has type int -> sign but is here
> used with type int
> >
> > Why the error in the last line?
>; >
> >
> Because sign_int has type int -> sign.
>; But before the '-', OCaml expects an int.
>
> try this:
>;
> # sign_int (-5);;
&gt;
> Salutations
>
> Matt
>
>
>
>
>

__________________________________________________________
Cheap talk?
Check out Yahoo! Messenger's low PC-to-Phone call rates.
http://voice.yahoo.com

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] sign_int (example from manual)
user name
2006-11-23 15:43:46

On Thu, Nov 23, 2006 at 07:16:17AM -0800, michael rice wrote:
&gt; I stumbled upon that same problem when I first began
>; looking at OCaml and discovered the same solution. But
> still, it seems weird that -5 is not an int without
&gt; wrapping it in parens.

It's basically because there is an ambiguity between the expression:

i-5 # assuming i is an integer

and the function application:

f-5 # assuming f is a function

since at parse time we don't know the types (parsing happens long
before types are worked out). In other languages function application
has parentheses around the parameters so there is no such ambiguity.

There is also a unary negation symbol ~- (tilde minus) which you might
find useful:

# sign_int ~-5;;
- : sign = Negative

Rich.

--
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Internet Marketing and AdWords courses - http://merjis.com/courses - NEW!
Merjis blog - http://blog.merjis.com - NEW!

__._,_.___
.

__,_._,___
[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )