Created
August 6, 2010 00:19
-
-
Save petermarks/510622 to your computer and use it in GitHub Desktop.
Notes for Johan Lindberg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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