3 ms·
I believe this has some issues if the operation isn't commutative. For example: blah 0 = [] blah n = (++) [n] (blah ((-) n 1)) -- blah 5 -> [5,4,3,2,1] bla
by Psyonic 16y ago
I believe this has some issues if the operation isn't commutative.
For example:
blah 0 = []
blah n = (++) [n] (blah ((-) n 1))
-- blah 5 -> [5,4,3,2,1]
blah2 0 acc = acc
blah2 n acc = blah2 ((-) n 1) ((++) [n] acc)
-- blah2 5 [] -> [1,2,3,4,5]
If we changed the tail to:
blah2 (n-1) ((++) acc [n])
it would work in this case, but then we've flipped the arguments, and I have a feeling we could come up with a function where simply flipping the arguments won't work, though I can't think of one at the moment.
- sophacles 16y agoGood point! It becomes hard to do mechanical transforms like I suggested when you have to keep track of properties like this for every function. Thanks!
- Psyonic 16y agoYa that's the problem. Here's what I originally intended to write, but couldn't think of how to do cons in haskell so I used list addition. With cons (:), you can't rewrite it with the arguments swapped, so no simple mechanical transform will be able to fix this. blah 0 = [] blah n = (:) n (blah ((-) n 1)) -- blah 5 -> [5,4,3,2,1] blah2 0 acc = acc blah2 n acc = blah2 ((-) n 1) ((:) n acc) -- blah2 5 [] -> [1,2,3,4,5]