caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Why can't I use constructors as functions?
@ 2001-07-30 18:51 Krishnaswami, Neel
  2001-08-03  8:16 ` Xavier Leroy
  0 siblings, 1 reply; 6+ messages in thread
From: Krishnaswami, Neel @ 2001-07-30 18:51 UTC (permalink / raw)
  To: 'caml-list@inria.fr'

Hello,

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
neelk@cswcasa.com
 
-------------------
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] 6+ messages in thread

end of thread, other threads:[~2001-08-06  8:21 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <200108031627.JAA19052@dhpc0010.pdx.intel.com>
2001-08-06  8:14 ` [Caml-list] Why can't I use constructors as functions? Xavier Leroy
2001-07-30 18:51 Krishnaswami, Neel
2001-08-03  8:16 ` Xavier Leroy
2001-08-03  9:19   ` Andreas Rossberg
2001-08-03 10:00   ` Markus Mottl
     [not found]   ` <9kffrd$72l$1@qrnik.zagroda>
2001-08-05 12:57     ` Marcin 'Qrczak' Kowalczyk

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