Browse thread
[Caml-list] Recursive apply function
-
Peter Scott
- Aleksey Nogin
- Brian Hurt
- Ville-Pertti Keinonen
[
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: | -- (:) |
| From: | Aleksey Nogin <nogin@c...> |
| Subject: | Re: [Caml-list] Recursive apply function |
On 19.11.2003 19:54, Peter Scott wrote: > I'm having an interesting disagreement with ocaml about types. I'm > trying to make a function to imitate the lisp apply function. That is, I > want to make a function which will apply another function to a list. > > I came up with this code: > > let rec apply f args = > match args with > arg :: rest -> apply (f arg) rest > | [] -> f;; > > let add x y = x + y;; > let x = apply add [2; 3];; > print_int x;; > > As you can see, the idea is to go recursively applying the function to > the first element of the list. Unfortunately, I get this message when > run: "This expression has type 'a but is here used with type 'b -> 'a", > referring to the "(f arg)" part. > > Is there a way around this? Well, note that your "apply" function would only make sense for fixed-length lists (e.g. "apply add [2; 3; 4]" is not right). In OCaml, fixed-length lists are called "tuples". ;-) -- Aleksey Nogin Home Page: http://nogin.org/ E-Mail: nogin@cs.caltech.edu (office), aleksey@nogin.org (personal) Office: Jorgensen 70, tel: (626) 395-2907 ------------------- 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