Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/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: while loop (Sylvain Henry)
2. Re: while loop (PICCA Frederic-Emmanuel)
3. Random Numbers with the State Monad (Thomas Jakway)
4. Re: Random Numbers with the State Monad (Nikita Kartashov)
5. Unequal types constraint (Imants Cekusins)
----------------------------------------------------------------------
Message: 1
Date: Thu, 11 Feb 2016 13:11:45 +0100
From: Sylvain Henry <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] while loop
Message-ID:
<capmptcuewmtswsgz6dgxt8uln6lmyraummgpjd3xkxpp60d...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hi,
In these cases, I write the explicitly recursive version (e.g. getList1)
and look for a pattern (e.g. getList2).
getList1 :: Ptr List -> IO [Ptr Item]
getList1 l = c_get_first_item l >>= go
where
go e
| e == nullPtr = return []
| otherwise = do
next <- c_get_next_item l e
es <- go next
return (e:es)
getList2 :: Ptr List -> IO [Ptr Item]
getList2 l = c_get_first_item l >>= unfoldrM go
where
go e
| e == nullPtr = return Nothing
| otherwise = do
next <- c_get_next_item l e
return (Just (e,next))
I hope it helps
Sylvain
2016-02-11 12:02 GMT+01:00 PICCA Frederic-Emmanuel <
[email protected]>:
> Hello,
>
> I am playing with FFI and I need to extract from a C API a list of
> pointers.
>
> I have two methods available
>
> c_get_first_item :: Ptr List -> IO (Ptr Item)
> c_get_next_item :: Ptr List -> Ptr Item -> IO (Ptr Item)
>
> I would like to obtain a [Ptr Item]
>
> I try to used whileM but I did not find how to inject the first item in
> the loop.
>
> whileM (return . ( /= nullPtr)) (c_get_next_item list item)
>
> the c_get_next_item return a nullPtr when there is no remaining item.
>
> what is the haskell way in order to extract a list of pointer using these
> C methods ?
>
> thanks for your help
>
> Frederic
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20160211/e623ab53/attachment-0001.html>
------------------------------
Message: 2
Date: Thu, 11 Feb 2016 12:14:56 +0000
From: PICCA Frederic-Emmanuel
<[email protected]>
To: "The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell" <[email protected]>
Subject: Re: [Haskell-beginners] while loop
Message-ID:
<a2a20ec3b8560d408356cac2fc148e53b3036...@sun-dag3.synchrotron-soleil.fr>
Content-Type: text/plain; charset="iso-8859-1"
> I hope it helps
Yes a lot :))
thanks you very much (merci beaucoup :p)
Fr?d?ric
------------------------------
Message: 3
Date: Thu, 11 Feb 2016 20:14:08 -0500
From: Thomas Jakway <[email protected]>
To: "[email protected]" ">>" The Haskell-Beginners Mailing List -
Discussion of primarily beginner-level topics related to Haskell
<[email protected]>
Subject: [Haskell-beginners] Random Numbers with the State Monad
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8; format=flowed
I'm having a bad time using the State monad to generate random numbers
without carrying around a lot of StdGens manually.
I have this snippet in the IO monad:
... IO stuff ...
gen <- getStdGen
let (numPlayers, numMatches) = (evalState genRandVariables gen) ::
(Integer, Integer)
... More IO stuff ...
where maxRandPlayers = 10 :: Integer
minRandMatches = 10 :: Integer
maxRandMatches = 100 :: Integer
genRandVariables = (do
np <- randomR (1, maxRandPlayers) --minimum 1 other player
nm <- randomR (minRandMatches, maxRandMatches)
return (np, nm)) :: State StdGen (Integer, Integer)
I get this error message:
test/Jakway/Blackjack/Tests/IntegrationTests/MatchTests.hs:53:23:
Couldn't match expected type ?StateT
StdGen
Data.Functor.Identity.Identity Integer?
with actual type ?g0 -> (Integer, g0)?
Probable cause: ?randomR? is applied to too few arguments
In a stmt of a 'do' block: np <- randomR (1, maxRandPlayers)
In the expression:
(do { np <- randomR (1, maxRandPlayers);
nm <- randomR (minRandMatches, maxRandMatches);
return (np, nm) }) ::
State StdGen (Integer, Integer)
test/Jakway/Blackjack/Tests/IntegrationTests/MatchTests.hs:54:23:
Couldn't match expected type ?StateT
StdGen
Data.Functor.Identity.Identity Integer?
with actual type ?g1 -> (Integer, g1)?
Probable cause: ?randomR? is applied to too few arguments
In a stmt of a 'do' block:
nm <- randomR (minRandMatches, maxRandMatches)
In the expression:
(do { np <- randomR (1, maxRandPlayers);
nm <- randomR (minRandMatches, maxRandMatches);
return (np, nm) }) ::
State StdGen (Integer, Integer)
What's really baffling to me is I feel like this is how it *should*
look--that the whole point of the state monad is to *not* have to
explicitly pass the StdGen to randomR. What am I doing wrong?
------------------------------
Message: 4
Date: Fri, 12 Feb 2016 14:38:01 +0300
From: Nikita Kartashov <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Random Numbers with the State Monad
Message-ID: <[email protected]>
Content-Type: text/plain; charset="windows-1252"
Hi!
Take a look at MonadRandom [1]. It is basically what you want without getting
generator explicitly.
[1] https://hackage.haskell.org/package/MonadRandom
With regards,
Nikita Kartashov
On 12 Feb 2016, at 04:14, Thomas Jakway <[email protected]> wrote:
> I'm having a bad time using the State monad to generate random numbers
> without carrying around a lot of StdGens manually.
> I have this snippet in the IO monad:
>
> ... IO stuff ...
> gen <- getStdGen
> let (numPlayers, numMatches) = (evalState genRandVariables gen) ::
> (Integer, Integer)
> ... More IO stuff ...
>
> where maxRandPlayers = 10 :: Integer
> minRandMatches = 10 :: Integer
> maxRandMatches = 100 :: Integer
> genRandVariables = (do
> np <- randomR (1, maxRandPlayers) --minimum 1 other player
> nm <- randomR (minRandMatches, maxRandMatches)
> return (np, nm)) :: State StdGen (Integer, Integer)
>
>
> I get this error message:
> test/Jakway/Blackjack/Tests/IntegrationTests/MatchTests.hs:53:23:
> Couldn't match expected type ?StateT
> StdGen Data.Functor.Identity.Identity
> Integer?
> with actual type ?g0 -> (Integer, g0)?
> Probable cause: ?randomR? is applied to too few arguments
> In a stmt of a 'do' block: np <- randomR (1, maxRandPlayers)
> In the expression:
> (do { np <- randomR (1, maxRandPlayers);
> nm <- randomR (minRandMatches, maxRandMatches);
> return (np, nm) }) ::
> State StdGen (Integer, Integer)
>
> test/Jakway/Blackjack/Tests/IntegrationTests/MatchTests.hs:54:23:
> Couldn't match expected type ?StateT
> StdGen Data.Functor.Identity.Identity
> Integer?
> with actual type ?g1 -> (Integer, g1)?
> Probable cause: ?randomR? is applied to too few arguments
> In a stmt of a 'do' block:
> nm <- randomR (minRandMatches, maxRandMatches)
> In the expression:
> (do { np <- randomR (1, maxRandPlayers);
> nm <- randomR (minRandMatches, maxRandMatches);
> return (np, nm) }) ::
> State StdGen (Integer, Integer)
>
> What's really baffling to me is I feel like this is how it *should*
> look--that the whole point of the state monad is to *not* have to explicitly
> pass the StdGen to randomR. What am I doing wrong?
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20160212/a2a6387e/attachment-0001.html>
------------------------------
Message: 5
Date: Fri, 12 Feb 2016 12:43:57 +0100
From: Imants Cekusins <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Unequal types constraint
Message-ID:
<CAP1qinZuisQUV+RG0PgE6J1rmWhQgByrONY3oqWM=qPgVU1k=a...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
class C a b
instance C Int a -- when a /= Int
instance C a a -- when a == a
how would you do this without IncoherentInstances?
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 92, Issue 14
*****************************************