[Haskell-beginners] nim

Kyle Murphy orclev at gmail.com
Mon Oct 26 14:59:16 EDT 2009


The type signature you provided for typeOfMove doesn't match the function
body.
"typeOfMove :: (a,b) -> [Int]" says that typeOfMove takes a tuple of two
different values, and returns a list of Ints, but the definition you
provided for typeOfMove only takes a list. I think perhaps what you want is
something more like:

import Data.Char (toLower)
import Control.Monad (liftM)

typeOfMove :: (Char, Int) -> [Int] -> [Int]
typeOfMove ('a', x) xs = zipWith (-) xs [x,0,0]
typeOfMove ('b', x) xs = zipWith (-) xs [0,x,0]
typeOfMove ('c', x) xs = zipWith (-) xs [0,0,x]

main :: IO ()
main = do
    putStrLn "Which pile (A,B,C)?"
    x <- liftM toLower $ getChar
    putStrLn ""
    putStrLn "How many stones?"
    y <- readLn
    let z = typeOfMove (x,y) [5,6,7]
    putStrLn . show $ z

Of course, another way to do much the same thing is the following, which has
the advantage that you don't get an exception inside the typeOfMove function
if the user passes in a value besides A, B, or C, although you do get an
exception when readLn attempts to perarse the value (which is better because
you can trap the exception there and re-ask for the correct value).
Similarly this version still isn't very good because it doesn't do any sort
of bounds checking on the number of stones to subtract, but I leave that as
an exercise to you.

data PileName = A | B | C deriving (Show, Eq, Read)

typeOfMove :: (PileName, Int) -> [Int] -> [Int]
typeOfMove (A, x) xs = zipWith (-) xs [x,0,0]
typeOfMove (B, x) xs = zipWith (-) xs [0,x,0]
typeOfMove (C, x) xs = zipWith (-) xs [0,0,x]

main :: IO ()
main = do
    putStrLn "Which pile A, B, or C (case matters)?"
    x <- readLn
    putStrLn "How many stones?"
    y <- readLn
    let z = typeOfMove (x,y) [5,6,7]
    putStrLn . show $ z


-R. Kyle Murphy
--
Curiosity was framed, Ignorance killed the cat.


On Mon, Oct 26, 2009 at 07:51, John Moore <john.moore54 at gmail.com> wrote:

> Hi All,
>           In the game of nim I want to make a move such as take x from
> pile A. This will then be subtracted from the starting list.Is this correct
> in as much as I want to have a move Take 3 from pile  A: input would be A3 I
> need it to return a list as I wish to take this result and use zipwith (-)
> [starting list][result]. How do I use the result, do I store in a variable?
>
> typeOfMove :: (a,b) -> [Int]
> typeOfMove ax
>   |ax = [x,0,0]
>   |bx = [0,x,0]
>   |cx = [0,0,x]
>
> John
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20091026/6f164a18/attachment.html


More information about the Beginners mailing list