[Haskell-cafe] Re: Seeking advice on a style question

Greg Buchholz haskell at sleepingsquirrel.org
Fri Dec 29 12:01:37 EST 2006


Steve Schafer wrote:
> 
> Here's the essence of the problem. If I have this:
> 
>  process1 x y =
>    let u = foo x y;
>        v = bar u;
>        w = baz v
>    in  w
> 
> I can easily rewrite it in point-free style:
> 
>  process1 = baz . bar . foo

    Not unless you have a much fancier version of function composition,
like...

    http://okmij.org/ftp/Haskell/types.html#polyvar-comp

> 
> But if I have this:
> 
>  process2 x y =
>    let u = foo x y;
>        v = bar u;
>        w = baz v u
>    in  w
> 
> then I can't avoid naming and using an intermediate variable. And that
> annoys me. The u in process2 is of no more value to me (pardon the
> pun) as the one in process1, but I am forced to use it simply because
> the data flow is no longer strictly linear.
> 
> The reason I brought up monads as a possible means of managing this
> problem is that the State, Reader and Writer monads already handle
> certain specific "shapes" of nonlinear data flow, which suggested to
> me that maybe there was a monadic approach to managing nonlinear data
> flow in a more general way. Of course, if there is a non-monadic,
> purely functional way to do it, that would be even better, but I've
> never seen such a thing (short of doing lots of tupling and
> un-tupling).

-- Use combinators which automate the tupling/un-tupling.
-- See also, the Joy language...
-- http://www.latrobe.edu.au/philosophy/phimvt/joy/j00rat.html

main = process2 test

process2 = baz . bar . dup . foo

foo = mul . (push 2) . mul

bar = rep . swap . (push 'A')

baz = add . len 

test = (2,(3,()))::(Int,(Int,()))


dup (a,b) = (a,(a,b))
swap (a,(b,c)) = (b,(a,c))
push a b = (a,b)

lift1 f (a,b) = (f a,b)
lift2 f (a,(b,c)) = (f a b,c)

len = lift1 length 
add = lift2 (+)
mul = lift2 (*)
rep = lift2 replicate




More information about the Haskell-Cafe mailing list