for :: STRef s a -> (a -> a) -> (a -> Bool) -> (a -> ST s b) -> ST s () for curr iter endTest action = do currVal <- readSTRef curr if (endTest currVal) then do action currVal modifySTRef curr iter for curr iter endTest action else return ()
example = do a <- newSTRef (0 :: Int) b <- newSTRef (1 :: Int) for a (+1) (<5) $ \a -> modifySTRef b (*2) aa <- readSTRef a bb <- readSTRef b return (aa, bb)
Name:
Anonymous2014-06-14 14:24
>>11 Your loop is lazy and slow. It doesn't compile into a single "loop label" opcode
Name:
Anonymous2014-06-14 14:34
Here, I've rewritten it a bit:
for :: a -> (a -> a) -> (a -> Bool) -> (a -> ST s b) -> ST s () for var step endTest action = do if (endTest var) then do action var for (step var) step endTest action else return ()
example = do b <- newSTRef (1 :: Int) for 0 (+1) (<5) $ \a -> modifySTRef b (*(a + 1)) res <- readSTRef b return res
Name:
Anonymous2014-06-14 14:43
>>12 I'll run some performance tests for ya in a while.
>>15 It can but they're not really representative as the exact running code will depend on strictness analysis, inline analysis and optimizations performed at the call site. Add to that the ambiguity of 3 code-generators and there's plenty of reasons why people mostly look at Core dumps (the desugared functional language) rather than Assembly dumps.
I guess compiling Haskell is even harder than compiling Scheme.
Name:
Anonymous2014-06-14 16:43
>>17 Scheme is easy. Haskell is easy if you don't care about efficiency.
Name:
Anonymous2014-06-14 16:59
Yep, you were right, it is slow. The Clang-compiled C for-loops do in 0.03 seconds what my Haskell for-loop does in 0.22 seconds. A 7x slowdown in a for loop? Crap.
Name:
Anonymous2014-06-14 17:14
Shit, that was without optimizations on the C side. With -O2 it's 0.024 sec vs 0.537 sec! Fucking 20x slowdown!
I can easily jump to 100x slowdown, if compiler fail to analyze continuations and replace them with jumps where possible. I remember the story about Algol compiler written in Algol, which was so slow, compiling 1-byte of source code took and minutes, because of lazy-evaluation.