Version française
Home     About     Download     Resources     Contact us    
Browse thread
list composition functions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Julian Assange <proff@i...>
Subject: list composition functions

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?