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

Juan Carlos Arevalo Baeza jcab.lists at JCABs-Rumblings.com
Thu Aug 25 13:27:24 EDT 2005


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



More information about the Haskell-Cafe mailing list