<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/11/a98ffd2d35498010d7a28ae7289a6827"
  from="Brian Hurt &lt;bhurt@s...&gt;"
  author="Brian Hurt"
  date="2003-11-20T06:32:12"
  subject="Re: [Caml-list] Recursive apply function"
  prev="2003/11/370c857e140e18498140b6387a2b7b68"
  next="2003/11/200096eaceaa7d8ec65f137c52c25c57"
  prev-in-thread="2003/11/461606c4d3157a5a76a31d3c6191a03b"
  next-in-thread="2003/11/a67170a9ced404324482eedfe2026978"
  prev-thread="2003/11/a2140a10ae8f0fe8f4ff398f0fbd2ddc"
  next-thread="2003/11/200096eaceaa7d8ec65f137c52c25c57"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] Recursive apply function">
<msg 
  url="2003/11/7e7da260aa46285715d49921794fa6a1"
  from="Peter Scott &lt;sketerpot@c...&gt;"
  author="Peter Scott"
  date="2003-11-20T03:32:19"
  subject="[Caml-list] Recursive apply function">
<msg 
  url="2003/11/461606c4d3157a5a76a31d3c6191a03b"
  from="Aleksey Nogin &lt;nogin@c...&gt;"
  author="Aleksey Nogin"
  date="2003-11-20T04:42:14"
  subject="Re: [Caml-list] Recursive apply function">
</msg>
<msg 
  url="2003/11/a98ffd2d35498010d7a28ae7289a6827"
  from="Brian Hurt &lt;bhurt@s...&gt;"
  author="Brian Hurt"
  date="2003-11-20T06:32:12"
  subject="Re: [Caml-list] Recursive apply function">
</msg>
<msg 
  url="2003/11/a67170a9ced404324482eedfe2026978"
  from="Ville-Pertti Keinonen &lt;will@e...&gt;"
  author="Ville-Pertti Keinonen"
  date="2003-11-20T10:45:53"
  subject="Re: [Caml-list] Recursive apply function">
</msg>
</msg>
</thread>

<contents>
On Wed, 19 Nov 2003, Peter Scott wrote:

&gt; I'm having an interesting disagreement with ocaml about types. I'm trying 
&gt; to make a function to imitate the lisp apply function. That is, I want to 
&gt; make a function which will apply another function to a list.
&gt; 
&gt; I came up with this code:
&gt; 
&gt; let rec apply f args =
&gt;    match args with
&gt;        arg :: rest -&gt; apply (f arg) rest
&gt;      | [] -&gt; f;;

This is untypeable.  What's the type of f?  It's a function that takes a 
'a and returns a function which takes a type 'a and returns a function 
which takes a type 'a and returns a function which takes a type 'a and 
etc.

What you want is List.fold_left, which has type:
('a -&gt; 'b -&gt; 'a) -&gt; 'a -&gt; 'b list -&gt; 'a

and can be implemented as:

let rec fold_left f init_val = function
	| [] -&gt; init_val
	| h :: t -&gt; fold_left f (f init_val h) t
;;

But don't reimplement it, use the library version.

&gt; 
&gt; let add x y = x + y;;
&gt; let x = apply add [2; 3];;
&gt; print_int x;;

let x = List.fold_left (+) 0 [2; 3];;

You don't need to define a special add function.

-- 
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
                                - Gene Spafford 
Brian

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

</contents>

</message>

