Version française
Home     About     Download     Resources     Contact us    
Browse thread
map and fold
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] map and fold
On Sun, 2006-12-24 at 00:54 +0100, Andrej Bauer wrote:
> skaller wrote:

> Furthermore if t is inductively defined, we can express map in terms of 
> fold. Examples:
> 
> 1) Lists:
> 
> type 'a list = Nil | Cons of 'a * 'a list
> 
> let map f = fold Nil (fun u _ x -> Cons (f u, x))
> 
> 2) Trees:
> 
> type 'a tree = Empty | Node of 'a * 'a tree * 'a tree
> 
> let map f = fold Empty (fun u _ _ x y -> Node (f u, x, y))

However in Ocaml at least you cannot actually write a single
definition for map in terms of a single fold -- you have to
write out fold for each data type, and worse, even given that
you still need to write out map for each data type too,
following an idiomatic pattern.

How could Ocaml be extended to get rid of this unsafe
verbosity?

Even if the resulting generic operators weren't first class,
it would still be useful to define 'map' once and be done
with it.

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net