DEV Community

Discussion on: Haskell by example - Utopian Tree

Collapse
 
theodesp profile image
Theofanis Despoudis

I find the whole line:

foldl (flip ($)) 1 . take n . cycle $ [(*2), (+1)]

the epitome of Haskell's failure to go into mainstream. The whole logic is drawn into a symbolic soup where you forget what you wrote 5 minutes ago and you have to go back and remember what you did.

In parallel here is an implementation in Racket Scheme and how more readable it is:

(define (get-height n)
  (if (= n 0)
      1
      (utopian-tree-iter 1 n 2)))

(define (utopian-tree-iter curr n height)
  (cond ((equal? curr n) height)
        ((even? curr) (utopian-tree-iter (+ curr 1) n (times2 height)))
        (else (utopian-tree-iter (+ curr 1) n (+ height 1) ))))

(define (times2 x) (* x 2))

Here functions have names and they are easier to understand.

Sometimes it pays to be a little bit more verbose.

Collapse
 
jvanbruegge profile image
Jan van Brügge • Edited

In my opinion the opposite is true. Having to name and assign takes your focus away from what the algorithm does and focuses on the data or helper functions instead, which is irrelevant. Writing your code mostly pointfree condenses your code to the actual algorithm only.

It's all about how used you are to thinking about functions and not the data that thr functions operate on.