caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Recursive apply function
@ 2003-11-20  3:54 Peter Scott
  2003-11-20  4:41 ` Aleksey Nogin
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Peter Scott @ 2003-11-20  3:54 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-11-20 10:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-20  3:54 [Caml-list] Recursive apply function Peter Scott
2003-11-20  4:41 ` Aleksey Nogin
2003-11-20  7:31 ` Brian Hurt
2003-11-20 10:45 ` Ville-Pertti Keinonen

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