Andrew Wagner wrote:
I've started a blog series on writing a chess engine in Haskell. I
just posted the second blog entry today:
http://sequence.complete.org/node/361

I suspect there's more work to be done on that function, though. It
seems like there should be a nice way to remove that flip in apply.
Any thoughts?

The trick is that you should always prefer zipWith over zip if you have the chanse, tuples make life harder.

Let's use some equational reasoning:

   foldl apply emptyGameState (zip funcs fields)
 =   {- fill in 'apply' -}
   foldl (flip (ap fst snd)) emptyGS (zip funcs fields)
 =   {- write it as a lambda function to make it clearer -}
   foldl (\y (f,x) -> f x y) emptyGS (zip funcs fields)
 =   {- split fold into a fold and a map -}
   foldl (\y fx -> fx y) emptyGS $ map (\(f,x) -> f x)
                                      $ (zip funcs fields)
 =   {- map . zip --> zipWith -}
   foldl (\y fx -> fx y) emptyGS $ zipWith (\f x -> f x) funcs fields
 =   {- use prelude functions -}
   foldl (flip ($)) emptyGS $ zipWith ($) funcs fields
 ~=  {- now, do you really want a foldl or will foldr do? -}
   foldr ($) emptyGS $ zipWith ($) funcs fields


You can now also write the function in pointfree style:

> loadFEN = foldr ($) emptyGameState
>         . zipWith ($) funcs
>         . words
>    where funcs = [parseBoard, parseCastleStatus]

Twan
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to