English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Recursive apply function
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-11-20 (03:32)
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?


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