Skip to content

Instantly share code, notes, and snippets.

@petermarks
Created August 6, 2010 00:19
Show Gist options
  • Save petermarks/510622 to your computer and use it in GitHub Desktop.
Save petermarks/510622 to your computer and use it in GitHub Desktop.
Notes for Johan Lindberg
In http://github.com/johanlindberg/langgeeks/commit/3e6249fcd36be6685cce2a6936da8dd2471cbd51
you have:
> process :: String -> (Char, [String])
> process filename = do contents <- readFile filename
> return (getPlayer contents, getBoard contents)
It looks like you want to read the file and extract the player and board to pass this on to
another function. All that is missing here is the IO in the type signature.
> process :: String -> IO (Char, [String])
> process filename = do contents <- readFile filename
> return (getPlayer contents, getBoard contents)
Then you could write findAllMoves as:
> findAllMoves :: String -> IO ()
> findAllMoves filename = do (player, board) <- process filename
> print (findMoves player board)
Both of these have to be "in IO" because they both do some IO.
Alternatively, you could factor it differently, separating the pure from the IO code:
> findAllMoves :: String -> [(Int, Int)]
> findAllMoves contents = findMoves (getPlayer contents) (getBoard contents)
> process :: String -> IO ()
> process filename = do contents <- readFile filename
> print (findAllMoves contents)
Or keep it all together being clear about the sub expressions:
> findAllMoves :: String -> IO ()
> findAllMoves filename = do contents <- readFile filename
> let player = getPlayer contents
> let board = getBoard contents
> let moves = findMoves player board
> print moves
I'm not sure which I like best. Your solution is perfectly fine, but I'm sensing you were
frustrated that you couldn't separate it out the way you wanted to.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment