|
|
>>> Not so much that, but rather the deep nesting that gets to me..
>
> You can break down in separate procedures/functions if you prefer. But
> deep nesting with anonymous functions is a hallmark of functional
> languages...
I would say that if you find yourself needing to nest things *too*
deeply, you're doing something wrong.
>> This is why Haskell allows you to take
>>
>> foo1(foo2(foo3(foo4(foo5(foo6(x))))))
>>
>> and rewrite it as
>>
>> foo1 $ foo2 $ foo3 $ foo4 $ foo5 $ foo6 x
>
> Haven't we gone through this before?
>
> (define (compose f g . fs)
> (if (null? fs) (lambda (x) (f (g x)))
> (apply compose (lambda (x) (f (g x))) (car fs) (cdr fs))))
>
> (define o compose)
>
> (define (inc n) (+ 1 n))
> (define (sqr n) (* n n))
>
> (o inc sqr inc) ; returns a procedure, which you may store somewhere and
> later reuse
>
> ((o inc sqr inc) 3) ; applies the procedure to argument 3
>
> so, you're example would be:
>
> ((o foo1 foo2 foo3 foo4 foo5 foo6) x)
> foo1 $ foo2 $ foo3 $ foo4 $ foo5 $ foo6 x
>
> whoa! It's shorter! ;)
What, defining three pages of code is longer than the 1-liner I posted?
The ($) operator is defined in the Haskell language standard. :-P
Post a reply to this message
|
|