Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Re: Built-in/more elegant version of this        function?
      (Jordan Cooper)
   2.  Re: Why is this type ambiguous? (Maur??cio CA)
   3.  nim (John Moore)
   4. Re:  nim (Deniz Dogan)
   5.  unix pkg on cygwin (MH)
   6. Re:  nim (Kyle Murphy)


----------------------------------------------------------------------

Message: 1
Date: Mon, 26 Oct 2009 16:48:25 -0700
From: Jordan Cooper <[email protected]>
Subject: [Haskell-beginners] Re: Built-in/more elegant version of this
        function?
To: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Am Montag 26 Oktober 2009 18:49:51 schrieb Jordan Cooper:
>> I was wondering if there is a built-in way to do what this function I
>> wrote does (or at least a more elegant way, preferably without
>> explicit recursion).
>>
>> f xss = map safeHead xss : f (map safeTail xss)
>>
>> (where safeHead/Tail are versions of head/tail that don't crash on []
>> input)
>
>Not quite the same, but
>
>Prelude> :m +Data.List
>Prelude Data.List> transpose [[1,2],[],[3,4,5]]
>[[1,3],[2,4],[5]]
>
>could be more or less what you want.

Thank you, I think that will work nicely!


------------------------------

Message: 2
Date: Mon, 26 Oct 2009 23:34:23 -0200
From: Maur??cio CA <[email protected]>
Subject: [Haskell-beginners] Re: Why is this type ambiguous?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

 >> But isn't the type of 'dummy' defined by 'return dummy' beeing
 >> a possible return value (and, so, dummy :: b)?

 > No. let-bindings are polymorphic, so dummy :: forall a. a

Oh, I once learned that some code of mine didn't work because
lambda bindings are monomorphic. Now I can use it for good.

    genericCast :: (Storable a, Storable b) => a -> IO b
    genericCast v = return undefined >>= \dummy ->
      allocaBytes (max (sizeOf v) (sizeOf dummy)) $ \p ->
        poke p v >> if False then return dummy else peek (castPtr p)

In ghci:

    > let a = 4 :: WordPtr
    > b <- genericCast a :: IO (Ptr ())
    > b
    0x00000004


Thanks!
Maurício



------------------------------

Message: 3
Date: Tue, 27 Oct 2009 12:39:55 +0000
From: John Moore <[email protected]>
Subject: [Haskell-beginners] nim
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi getting there with nimprogram well have it working in different areas.

The last part is where the most trouble is

import Control.Monad

import System.Random



initNim :: IO [Int]

initNim = replicateM 3 $ randomRIO (1,10)--- This get the random numbers





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 ?"

    x <- readLn

    putStrLn "How many stones?"

    y <- readLn

    let z = typeOfMove (x,y) [9,9,9]-- cannot get the random numbers here

    putStrLn . show $ z





 *This is where the main problem is I 'm trying to run the game?*

play nim = do

  z <- getLine

             newAnswer <- return (diff z)

if newAnswer == [0,0,1]||[0,1,0]||[1,0,0]

                 then putStrn "You win"

                 else play nim newAnswer



diff z ws hs =[ if z==w then w else h]-- trying to return different list
here





John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20091027/7aca1600/attachment-0001.html

------------------------------

Message: 4
Date: Tue, 27 Oct 2009 13:49:10 +0100
From: Deniz Dogan <[email protected]>
Subject: Re: [Haskell-beginners] nim
To: John Moore <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

2009/10/27 John Moore <[email protected]>:
> Hi getting there with nimprogram well have it working in different areas.
>
> initNim :: IO [Int]
> initNim = replicateM 3 $ randomRIO (1,10)--- This get the random numbers
> data PileName = A | B | C deriving (Show, Eq, Read)
>
> main = do
>     putStrLn "Which pile A, B, or C ?"
>     x <- readLn
>     putStrLn "How many stones?"
>     y <- readLn
>     let z = typeOfMove (x,y) [9,9,9]-- cannot get the random numbers here
>     putStrLn . show $ z

If I understand this part of your problem correctly, try this:

main = do
    putStrLn "Which pile A, B, or C ?"
    x <- readLn
    putStrLn "How many stones?"
    y <- readLn
    nums <- initNim  -- Get the three random numbers
    let z = typeOfMove (x,y) nums -- Pass them along
    putStrLn . show $ z

The same effect can be achieved in a number of different ways, but
this should be fairly clear. (I haven't tested this by the way, so
excuse any typos and bugs.)

-- 
Deniz Dogan


------------------------------

Message: 5
Date: Tue, 27 Oct 2009 10:31:15 -0400
From: MH <[email protected]>
Subject: [Haskell-beginners] unix pkg on cygwin
To: "fa.haskell abridged email subscribers"
        <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

I tried to use unix package on Cygwin and it didn't work. I found a
far better solution, use Sun's VirtualBox from virtualbox.org.
It runs a virtual OS on your host operating system and it works very
well. I installed virtualbox on Windows host, and I am running Ubuntu
inside virtualbox with Haskell on it. Works like charm. Vitualbox like
VMWare, except it is free.

Malik


------------------------------

Message: 6
Date: Tue, 27 Oct 2009 11:39:52 -0400
From: Kyle Murphy <[email protected]>
Subject: Re: [Haskell-beginners] nim
To: John Moore <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

I'm still not sure you're thinking about this the right way. Maybe it would
help to see a working version (although not a *good* working version).
This e-mail is a literate haskell program, copy everything from the next
line till the EOF into nim.lhs and run it.
----
Import the modules we need.

> import Control.Monad
> import System.Random

Define the types we're going to be using.

> data PileName = A | B | C deriving (Show, Eq, Read)
> type State = [Int]
> type Move = (PileName, Int)

initNim setups a new random game State.

> initNim :: IO State
> initNim = replicateM 3 $ randomRIO (1,10)

doMove takes a Move, and a initial State
and returns the updated game State after the Move.

> doMove :: Move -> State -> State
> doMove (A,x) xs = zipWith (-) xs [x,0,0]
> doMove (B,x) xs = zipWith (-) xs [0,x,0]
> doMove (C,x) xs = zipWith (-) xs [0,0,x]

getMove returns the next Move.

> getMove :: IO Move
> getMove = do
>     putStrLn "Which pile A, B, or C?"
>     x <- readLn
>     putStrLn "How many stones?"
>     y <- readLn
>     return (x,y)

checkWin takes a State and returns whether the game
has been won or not.

> checkWin :: State -> Bool
> checkWin [1,0,0] = True
> checkWin [0,1,0] = True
> checkWin [0,0,1] = True
> checkWin _       = False

doRound takes the current game State, performs one
round of play, and returns the updated State.

> doRound :: State -> IO State
> doRound oldState = do
>     putStrLn . show $ oldState
>     move <- getMove
>     let newState = doMove move oldState
>     return newState

untilM is a little helper function. To understand
what it does take a look at the until function
defined in Prelude. untilM does the same thing
but inside of a Monad (in this case IO). The
short version is that it runs the second function
it's given until the first function returns True.

> untilM :: (Mona m) => (a -> Bool) -> (a -> m a) -> a -> m a
> untilM p f x | p x = return x | otherwise = f x >>= untilM p f

The main ties everything together by initializing
the starting state, and then kicking off the
doRound loop (via untilM).

> main :: IO ()
> main = do
>     state <- initNim
>     untilM checkWin doRound state
>     putStrLn "You win!"

Areas for improvement include the following:

No error checking on user input, if you enter an invalid pile or
something that isn't a number (or is more then the number
of stones in the pile) bad things happen (hint: lookup try
catch and exception handling tutorials)

The checkWin function, the doMove, and the doRound
function don't do any sort of check to make sure the number
of stones removed can actually be removed (that is, none
of the piles go negative), nor is there a check done for
if *all* stones get removed.

It might be educational to change the type of State from
[Int] to (Int, [Int]) with the first value being the number of the
player who's turn it is. Adjust the various functions to
accept the new type of State and to update it appropriately.

----
EOF

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


On Tue, Oct 27, 2009 at 08:39, John Moore <[email protected]> wrote:

> Hi getting there with nimprogram well have it working in different areas.
>
> The last part is where the most trouble is
>
> import Control.Monad
>
> import System.Random
>
>
>
> initNim :: IO [Int]
>
> initNim = replicateM 3 $ randomRIO (1,10)--- This get the random numbers
>
>
>
>
>
> 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 ?"
>
>     x <- readLn
>
>     putStrLn "How many stones?"
>
>     y <- readLn
>
>     let z = typeOfMove (x,y) [9,9,9]-- cannot get the random numbers here
>
>     putStrLn . show $ z
>
>
>
>
>
>  *This is where the main problem is I 'm trying to run the game?*
>
> play nim = do
>
>   z <- getLine
>
>              newAnswer <- return (diff z)
>
> if newAnswer == [0,0,1]||[0,1,0]||[1,0,0]
>
>                  then putStrn "You win"
>
>                  else play nim newAnswer
>
>
>
> diff z ws hs =[ if z==w then w else h]-- trying to return different list
> here
>
>
>
>
>
> John
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20091027/1af0874f/attachment.html

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 16, Issue 22
*****************************************

Reply via email to