Functional ‘while’ loops – no, really
billwadge.wordpress.com
Functional ‘while’ loops – no, really
1–8 of 8 posts
Re: Functional ‘while’ loops – no, really
#2I 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.
Re: Functional ‘while’ loops – no, really
#3 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.Re: Functional ‘while’ loops – no, really
#4What happens if the loop body does not assign to the result variable? What if the condition is false before entering the loop? I 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 (((let name args body) is essentially sugar for creating a function and calling it with some default args)
Re: Functional ‘while’ loops – no, really
#5What happens if the loop body does not assign to the result variable? What if the condition is false before entering the loop? I 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.
My understanding is that a while (or a valof) that doesn't assign to result is invalid. 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 ( ((let name args body) is essentially sugar for creating a function and calling it with s…
Re: Functional ‘while’ loops – no, really
#6What happens if the loop body does not assign to the result variable? What if the condition is false before entering the loop? I 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.
My understanding is that a while (or a valof) that doesn't assign to result is invalid. 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 ( ((let name args body) is essentially sugar for creating a function and calling it with s…
Looks like I fooled some people into assuming that whiles are imperative. They're not
Re: Functional ‘while’ loops – no, really
#7Re: Functional ‘while’ loops – no, really
#8Haskell sucks, but they don't even know Haskell. There's a package ( https://hackage.haskell.org/package/monad-loops ) which contains `whileM`, a pure while-loop which works with any Monad.
Mine have multiple variables, no monads, and no side effects - it all gets done by tail recursion.