
Bayley, Alistair wrote:
From: Juan Carlos Arevalo Baeza
If I don't want to pay for that cost, then I have to convert the code as in:
func p = b <- peekByteOff p 4 case b of B1 -> doSomething B2 -> do { a <- peekByteOff p 0; doSomethingElse a } B3 -> do { a <- peekByteOff p 0; doAnotherThing a }
Repetition, repetition, repetition. This does get tedious.
Would this work?
Well... yes. But I'm looking for a purely functional and lazy solution to something that's, underneath it all, purely functional and could be done lazily. Your solution below just avoids some typing, it doesn't avoid the repetition.
func p = do let get_a = peekByteOff p 0 b <- peekByteOff p 4 case b of B1 -> doSomething B2 -> do a <- get_a; doSomethingElse a B3 -> do a <- get_a; doAnotherThing a
Or, with marginally less duplication:
B2 -> get_a >>= doSomethingElse B3 -> get_a >>= doAnotherThing
It is intriguing, though. Thanx! JCAB