Re: How to escape the Ocaml's superfluous parentheses and type declarations?
- From: Remi Vanicat <invalid.newscomp.10.vanicat@xxxxxxxxxxxxxxx>
- Date: Thu, 8 Sep 2005 20:45:18 +0000 (UTC)
andrewspencers@xxxxxxxxx writes:
> Huh? Let's investigate:
>
> # Mycons (1, Mycons (2, Mycons (3,E)));;
> - : int mylist = Mycons (1, Mycons (2, Mycons (3, E)))
> # let foo = Mycons (1, Mycons (2, Mycons (3,E))) in mylength foo;;
> - : int = 3
> # mylength (Mycons (1, Mycons (2, Mycons (3,E))));;
> - : int = 3
>
> It demands parentheses?! What is this; Lisp?
The real question is "why constructor are not curryfied when function
are ?". The parser have no information on type, and so read
mylength Mycons (1, Mycons (2, Mycons (3,E)));;
as
(mylength Mycons) (1, Mycons (2, Mycons (3,E)));;
> Anyway, onward:
>
> # mylength (Mycons (3, Mycons ('a', E)));;
> This expression has type char but is here used with type int
>
> I thought that Ocaml was supposed to be able to infer types without my
> explicitly specifying them... why can't it infer the union of the types
> char and int?
Ocaml don't do what you want. There is no "union of char and
int". Notice that char and int have the same runtime representation.
More precisely Ocaml have no real union, but variant type that can be
seen as disjoint union.
> Anyway, I'll define the union explicitly, so I can continue onward:
>
> # type charint = char | int;;
> Syntax error
> # type charint = Char of char | Int of int;;
> type charint = Char of char | Int of int
>
> Ok, so I have to type the type name twice...
Char is not a type, its a constructor. You may have wrote
type charint = Foo of char | Bar of int
> Onward:
>
> # mylength (Mycons (3, Mycons ('a', E)));;
> This expression has type char but is here used with type int
>
> I've explicitly defined the union, and it STILL can't figure it out!
You have to wrote the constructor you use. always.
> Let's make it even more explicit:
>
> # mylength (Mycons (Int 3, Mycons (Char 'a', E)));;
> - : int = 2
>
> Why is that necessary?
>
> # quit;;
> Unbound value quit
> # exit;;
> - : int -> 'a = <fun>
exit 0 work. The same for #quit;; this is written is the documentation.
(eof (Control-D) work too).
Notice that there is an ocaml beginner mailing list.
http://groups.yahoo.com/group/ocaml_beginners/
--
Rémi Vanicat
.
- References:
- How to escape the Ocaml's superfluous parentheses and type declarations?
- From: andrewspencers
- How to escape the Ocaml's superfluous parentheses and type declarations?
- Prev by Date: Re: sml for newbies
- Next by Date: Re: sml for newbies
- Previous by thread: How to escape the Ocaml's superfluous parentheses and type declarations?
- Next by thread: Re: How to escape the Ocaml's superfluous parentheses and type
- Index(es):
Relevant Pages
|