Browse thread
[Caml-list] Recursive apply function
- Peter Scott
[
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: | Peter Scott <sketerpot@c...> |
| Subject: | [Caml-list] Recursive apply function |
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?
-Peter
-------------------
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