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

Bayley, Alistair Alistair_Bayley at ldn.invesco.com
Thu Aug 25 03:29:13 EDT 2005


> 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.
*****************************************************************



More information about the Haskell-Cafe mailing list