On Dec 23, 2003, at 1:53 AM, Dustin Sallings wrote: > > On Dec 22, 2003, at 22:26, Tyler Eaves wrote: > > (I'm sending this back to the list because, as you pointed out, there > were a few problems with that example, so here's one for the > archives). > >> Okay, so far so good. I take it that in O'Caml the overhead of a >> function call is much less than in, say, C? Otherwise this approach >> would seem to be rather inefficient. > > Well, the short answer is that that's not a function call. :) See > tail-recursion. > > It's also not the best example since it was so simple and didn't > return anything useful. Consider this implementation of fold (roughly > translating it in this email from a scheme version I wrote on my > palm): > > let rec fold f init lst = > match lst with > [] -> init > | h::t -> fold f (f init h) t > ;; > > If you're not familiar with fold, here's an example: > > fold (fun v c -> c + v) 0 [1;2;3;4;5;6;7;8;9;10] > > That invocation returns 55. Basically, the function is called for > each value in the list (v) and the current value is passed in along > with the current value during the calculation (c). When it gets to > the end (list is []), you return the init value. Otherwise, you apply > the function. > > Note that this function could also be written as follows (and > probably would be): > > let rec fold f init = function > [] -> init > | h::t -> fold f (f init h) t > ;; This makes since in general. I'm guess h and t correspond to the head and tail of the lift. It'll probably make more sense tomorrow. It's 2:11 here, and I'd planned to be asleep two hours ago. The replies have kept me up ;) Based on what I've learned, I've tried to write a simple prime number finder. It doesn't actually *work* or anything (Syntax Error on line 23, can't figu