English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Why can't I use constructors as functions?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-07-30 (18:47)
From: Krishnaswami, Neel <neelk@c...>
Subject: [Caml-list] Why can't I use constructors as functions?

I'm curious as to the reason why I can't use a datatype constructor
as a function. Eg, in SML I can write a function like this:

  datatype peano = Zero | Succ of peano

  fun fold succ zero n = 
    case n of
      Zero => zero
    | (Succ n') => succ (fold n')

  fun add a b = fold Succ a b (* Use the Succ constructor as a funtion *)

If I try something similar in Caml, 

  type peano = Zero | Succ of peano

  let rec fold succ zero n = 
    match n with
      | Zero -> zero
      | Succ(n') -> succ (fold succ zero n')

I can't write an add function like I can in SML:

  # let add a b = fold (Succ) a b;;
  Characters 20-24:
  The constructor Succ expects 1 argument(s),
  but is here applied to 0 argument(s)

Instead I need to wrap it in a function:

  # let add a b = fold (fun x -> Succ x) a b
  val add : peano -> peano -> peano = <fun>

Why was this design choice made? 

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