Skip to content

Instantly share code, notes, and snippets.

@rknuu
Last active August 29, 2015 14:14
Show Gist options
  • Save rknuu/c54dbc55228cb0b11ecb to your computer and use it in GitHub Desktop.
Save rknuu/c54dbc55228cb0b11ecb to your computer and use it in GitHub Desktop.
Why Haskell is cool
-- Taken from https://www.youtube.com/watch?v=6KkF5-_erns#t=1154
-- Original code taken from https://github.com/haskelllive/haskelllive/blob/episode-1/Chess.hs
module Chess where
-- This call reads in a string grid of a chessboard with each row separated by a newline
-- and returns an encoded board where each position could be Nothing or an encoded piece
-- type (returned from readSquare).
--
readBoard :: String -> Maybe Board
readBoard = mapM readRows . lines
where readRows = mapM readSquare
-- This does the exact same thing, only using function composition to eliminate
-- unnecessary inner definitions.
readBoard = (mapM . mapM) readSquare . lines
-- Gotta love function compositions!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment