RE: [Haskell-cafe] How lazy can "peek" be?

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?
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
Alistair. ----------------------------------------- ***************************************************************** Confidentiality Note: The information contained in this message, and any attachments, may contain confidential and/or privileged material. It is intended solely for the person(s) or entity to which it is addressed. Any review, retransmission, dissemination, or taking of any action in reliance upon this information by persons or entities other than the intended recipient(s) is prohibited. If you received this in error, please contact the sender and delete the material from any computer. *****************************************************************

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
participants (2)
-
Bayley, Alistair
-
Juan Carlos Arevalo Baeza