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

* Re: [Caml-list] Recursive apply function
  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
  2 siblings, 0 replies; 4+ messages in thread
From: Aleksey Nogin @ 2003-11-20  4:41 UTC (permalink / raw)
  To: Peter Scott, Caml List

On 19.11.2003 19:54, Peter Scott wrote:

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

Well, note that your "apply" function would only make sense for 
fixed-length lists (e.g. "apply add [2; 3; 4]" is not right). In OCaml, 
fixed-length lists are called "tuples". ;-)

-- 
Aleksey Nogin

Home Page: http://nogin.org/
E-Mail: nogin@cs.caltech.edu (office), aleksey@nogin.org (personal)
Office: Jorgensen 70, tel: (626) 395-2907

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

* Re: [Caml-list] Recursive apply function
  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
  2 siblings, 0 replies; 4+ messages in thread
From: Brian Hurt @ 2003-11-20  7:31 UTC (permalink / raw)
  To: Peter Scott; +Cc: caml-list

On Wed, 19 Nov 2003, Peter Scott wrote:

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

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 -> 'b -> 'a) -> 'a -> 'b list -> 'a

and can be implemented as:

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

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

> 
> let add x y = x + y;;
> let x = apply add [2; 3];;
> 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


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

* Re: [Caml-list] Recursive apply function
  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
  2 siblings, 0 replies; 4+ messages in thread
From: Ville-Pertti Keinonen @ 2003-11-20 10:45 UTC (permalink / raw)
  To: Peter Scott; +Cc: caml-list

On Wed, Nov 19, 2003 at 08:54:08PM -0700, Peter Scott wrote:

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

You can get your "apply" function to typecheck using the -rectypes option,
but it still doesn't do what you want; it only accepts functions that
return functions of the same (recursive) type.

Static typing prevents things like Lisp apply from being implemented
even for the limited case where arguments have the same type.  The type
of a partially applied function is almost never identical to the fully
applied function (except for the case of recursively typed functions that
return functions, such as your "apply").

Note that the -rectypes option is probably not something you want to use
unless you know that you need it.  An example is if you want to have a
state machine with functions representing states without resorting to
side-effects (f : event -> action * 'a as 'a).

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