Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* list composition functions
@ 2000-10-15 23:42 Julian Assange
  2000-10-16  6:57 ` Jacques Garrigue
  2000-10-16  7:58 ` Alain Frisch
  0 siblings, 2 replies; 4+ messages in thread
From: Julian Assange @ 2000-10-15 23:42 UTC (permalink / raw)
  To: caml-list; +Cc: proff


Imagine you have the follow three functions,

let mirror x = [x;x]
let plus1 x = [x+1]
let none x = []

I'm trying to define an operator (>>) that will then operate like so

(mirror >> mirror >> plus1) [1]

[2;2;2;2]

let (>>) a b = function x ->
  let rec loop = function
      [] -> []
    | hd::tl ->
	let rec loop2 = function
	    [] -> loop tl
	  | hd::tl -> b hd @ loop2 tl
	in
	  loop2 (a hd)
  in
    loop x

# (>>);;
- : ('a -> 'b list) -> ('b -> 'c list) -> 'a list -> 'c list = <fun>

While this looks okay, and works fine for two applications, the list
type keeps on growing with each partial application.

plus1 >> plus1 >> plus1 >> plus1;;
- : int list list list -> int list = <fun>
# 

Is there anyway I can prevent this, short of making plus1, etc symmetric
with respect to their argument types?



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

* Re: list composition functions
  2000-10-15 23:42 list composition functions Julian Assange
@ 2000-10-16  6:57 ` Jacques Garrigue
  2000-10-16  7:58 ` Alain Frisch
  1 sibling, 0 replies; 4+ messages in thread
From: Jacques Garrigue @ 2000-10-16  6:57 UTC (permalink / raw)
  To: proff; +Cc: caml-list

From: Julian Assange <proff@iq.org>

> Imagine you have the follow three functions,
> 
> let mirror x = [x;x]
> let plus1 x = [x+1]
> let none x = []
> 
> I'm trying to define an operator (>>) that will then operate like so
> 
> (mirror >> mirror >> plus1) [1]
> 
> [2;2;2;2]
> 
> let (>>) a b l = List.flatten (List.map b (List.flatten (List.map a l)))
> 
> # (>>);;
> - : ('a -> 'b list) -> ('b -> 'c list) -> 'a list -> 'c list = <fun>
> 
> While this looks okay, and works fine for two applications, the list
> type keeps on growing with each partial application.
> 
> plus1 >> plus1 >> plus1 >> plus1;;
> - : int list list list -> int list = <fun>
> # 
> 
> Is there anyway I can prevent this, short of making plus1, etc symmetric
> with respect to their argument types?

The problem is that your (a >> b) does not bear any symmetry with the
argument functions.
I can see at least two candidates for this.
1) The bind of the non-determinism monad:
   # let (>>) l f = List.flatten (List.map f l);;
   val ( >> ) : 'a list -> ('a -> 'b list) -> 'b list = <fun>
   # [1] >> mirror >> mirror >> plus1;;
   - : int list = [2; 2; 2; 2]
2) Flattening composition
   # let (>>) a b x = List.flatten (List.map b (a x));;
   val ( >> ) : ('a -> 'b list) -> ('b -> 'c list) -> 'a -> 'c list = <fun>
   # (mirror >> mirror >> plus1) 1;;
   - : int list = [2; 2; 2; 2]

They both slightly differ from what you were asking for, but I see no
other way to proceed as long as your basic blocks have type ('a -> 'b
list).

Jacques Garrigue



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

* Re: list composition functions
  2000-10-15 23:42 list composition functions Julian Assange
  2000-10-16  6:57 ` Jacques Garrigue
@ 2000-10-16  7:58 ` Alain Frisch
  2000-10-16 12:22   ` Julian Assange
  1 sibling, 1 reply; 4+ messages in thread
From: Alain Frisch @ 2000-10-16  7:58 UTC (permalink / raw)
  To: Julian Assange; +Cc: caml-list

On 16 Oct 2000, Julian Assange wrote:
> Imagine you have the follow three functions,
> 
> let mirror x = [x;x]
> let plus1 x = [x+1]
> let none x = []
> 
> I'm trying to define an operator (>>) that will then operate like so
> 
> (mirror >> mirror >> plus1) [1]
> 
> [2;2;2;2]

You could try something like:

let build_transformer f x =
    List.concat (List.map f x)

let (>>) t1 t2 x =
    t1 (t2 x)

let mirror = build_transformer (fun x -> [x;x])
let plus1  = build_transformer (fun x -> [x+1])
let none   = build_transformer (fun x -> [])


(mirror and none are not generalized since their definition
is expansive)


-- 
  Alain Frisch



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

* Re: list composition functions
  2000-10-16  7:58 ` Alain Frisch
@ 2000-10-16 12:22   ` Julian Assange
  0 siblings, 0 replies; 4+ messages in thread
From: Julian Assange @ 2000-10-16 12:22 UTC (permalink / raw)
  To: Alain Frisch; +Cc: Julian Assange, caml-list

> On 16 Oct 2000, Julian Assange wrote:
> 
> You could try something like:
> 
> let build_transformer f x =
>     List.concat (List.map f x)
> 
> let (>>) t1 t2 x =
>     t1 (t2 x)

The problem with this is that it builds an intermediary list at each stage,
before handing it to the next. The function that I used does not.

Cheers,
Julian.



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

end of thread, other threads:[~2000-10-16 15:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-10-15 23:42 list composition functions Julian Assange
2000-10-16  6:57 ` Jacques Garrigue
2000-10-16  7:58 ` Alain Frisch
2000-10-16 12:22   ` Julian Assange

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox