[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2003-11-17 (21:30) |
From: | chris.danx <chris.danx@n...> |
Subject: | Re: [Caml-list] Closure & Ref |
Brian Hurt wrote: > Since you're not setting the reference, why have one? Instead, try: > > let prodAdd x = fun y -> x + y > > But we can do it simpler than that: > > let prodAdd x y = x + y > > And use partial function application. (prodAdd 4) returns a function > which adds 4 to whatever int parameter passed to it. I didn't know OCaml had partial function application, thanks! I have programmed in Haskell before, but never got seriously into it (the things I wanted to do like IO where "advanced" in Haskell, whereas they're basic to me. That's one reason I want to learn OCaml, it doesn't so hard to do trivial interaction). > This allows you to have multiple different lists being constructed > independently. Note, the above code is actually more generic than it > looks- I had to add an explicit type statement to make it "come out > correct". Without the explicit type information: > > let make_listacc () = > let r = ref [] in > let acc x = r := x :: !r > and lst () = !r > in acc, lst > ;; > > The function is both clearer than the original, and creates lists of any > type, not just ints. If you hear me bitching about C++ and Java making > generics "special and extraordinary", this is a classic example of what > I'm kvetching about. I hear you. From my previous spell in FP I really got to like the ease of "generics" which are inconvient in C++ and a pain in Ada. GUIs and IO was just easier. [snip] > If you want to access specific members of a list (other than the head), I > wouldn't recommend using a list, but instead some other datastructure (an > array or hash table being the obvious choices). It was just a toy example to see if I could get that aspect of this idea to work in OCaml. Hmm... Thanks! ------------------- 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/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners