I wouldn't use the word "assign" these are definitions and their order is not significant. You're right, a while without a definition of result produces a syntax error.
If the condition is initially false, the definition of result is immediately evaluated.
Looks like I fooled some people into assuming that whiles are imperative. They're not
The author suggests that Haskell could benefit from rewriting
nodups = dedup [] where
dedup m [] = m
dedup m (x:xs) = dedup (if x `elem` m then m else m++[x]) xs
as some syntactic sugar while loop:
nodups l = while k != []
k = l ||| tail l
m = [] ||| if head k `elem` m then m else m ++ head k
result = m
but I would disagree. Btw, the former appears to suffer from quadratic slowdown due to repeated use of ++ which can be avoided by replacing m++[x] with x:m and making dedup m [] = reverse m.
I assume 'they' means me.
True I don't know Haskell but from what I can tell these loops are a far cry from what I'm proposing. One variable, monads, and apparently side effects.
Mine have multiple variables, no monads, and no side effects - it all gets done by tail recursion.
7 comments
[ 3.4 ms ] story [ 30.6 ms ] threadI think it would have been cleaner to pass the value the loop should return to a break primitive and otherwise compute it in a dedicated block.
If the condition is false before entering the loop then result would be 1 (its initial value)
if it helps and if I understand correctly, this would be the same algorithm expressed in scheme:
(define (fib n) (let loop ([i 1] [f 1] [pf 1]) (if (< i n) (loop (+ i 1) (+ f pf) f ) f )))
((let name args body) is essentially sugar for creating a function and calling it with some default args)
Looks like I fooled some people into assuming that whiles are imperative. They're not
Mine have multiple variables, no monads, and no side effects - it all gets done by tail recursion.