[Haskell-cafe] Number 1, at least for now

Ben Lippmeier Ben.Lippmeier at anu.edu.au
Wed Feb 1 22:01:28 EST 2006


Donald Bruce Stewart wrote:

>>Ha! I don't think "pure lazy fp" would be the phrase I would choose to 
>>describe this code.
>>
>>An example (from fannkuch):
>>
>>t <- IO $ \s ->
>>     case readIntOffAddr# p1# 0# s of
>>       (# s, p1 #) -> case readIntOffAddr# p1# (n# -# 1#) s of
>>           (# s, pn #) -> (# s, not (p1 ==# 0# || pn ==# (n# -# 1#)) #)
> 
> Ok, I'll bite ;) This is a very atypical "example", as fannkuch is the
> only entry written this way (and it could be rewritten, with careful
 > attention to the Core). There are also many lovely pure, lazy entries.


Not so atypical.

More examples (I didn't look /that/ hard.. :))

-----------------------------------
* From reverse-complement

reverseit iubc strand i l s =
     if i >=# l
         then (# s, (I# i, I# l) #)
         else case readWord8OffAddr# strand i s  of { (# s, c #) ->
              case readWord8OffAddr# strand l s  of { (# s, x #) ->
              case readWord8OffAddr# iubc (word2Int# x) s  of { (# s, y#)
[snip]


* From k-nucleotide.

eqmem i ptr1 ptr2 s = if i ==# 0# then (# s , True #) else
     case readInt8OffAddr# ptr1 0# s of { (# s, i8a #) ->
     case readInt8OffAddr# ptr2 0# s of { (# s, i8b #) ->
     if i8a ==# i8b
         then eqmem (i -# 1#) (plusAddr# ptr1 1#) (plusAddr# ptr2 1#) s
         else (# s, False #) } }


* From n-body.

kineticE i = let i' = (.|. (shiftL i 3))
              in do m <- mass i
                    vx <- unsafeRead b (i' vx)
                    vy <- unsafeRead b (i' vy)
                    vz <- unsafeRead b (i' vz)
                    return $! 0.5 * m * (vx*vx + vy*vy + vz*vz)

----------------------

*I* am certainly not implying that Haskell is anything less than the 
most wonderous language in the entire world.

I'm saying that there's a stark difference in style between the programs 
submitted to the shootout, and the ones I would show to people that I 
myself was trying to introduce to the wonders of purely lazy functional 
programming. :).

I think there's a big-fat-lesson about the tension between abstraction 
and implementation in these entries.

On one hand we've got "This is what I want", on the other it's "What do 
I have to do to implement it".


Ben.





More information about the Haskell-Cafe mailing list