Browse thread
Re: [Caml-list] Applications written in O'Caml
[
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: | Remi VANICAT <vanicat@l...> |
| Subject: | Re: [Caml-list] Simple question |
Eric Merritt <cyberlync@yahoo.com> writes:
> let me start off by saying that if this is not the
> place to post this type of question please direct me
> to the correct list/group. I am picking up ocaml,
> starting off by doing a few simple things. I am at
> somewhat of an impass here in that I cannot figure out
> why the function below gives me type errors. It works
> fine if I leave it non-tail recursive, but as soon as
> I put in an accumulator and make it tail-recursive it
> fails to compile. If one of you could please point me
> in the right direction for solving this problem that
> would be great. btw this is only for my own
> edification.
>
> let rec apply_fun_list x f_list accm =
> match f_list with
> h::t ->
> apply_fun_list x t accum::(h x)
> | [] ->
> accum::[];;
:: is the constructor that add something at the beginning of the
list.
so you may want to say :
let rec apply_rev_fun_list x f_list accum =
match f_list with
| h::t ->
apply_fun_list x t (h t)::accum
| [] -> accum
but there, the function will reverse the list. It might not be what
you want (but List.rev is tail recursive)
by the way, I would have wrote it like :
let rec apply_fun_list x f_list =
List.map (fun h -> h x) f_list
but List.map is not tail recursive....
--
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
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