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: filterM function (Sylvain Henry)
2. Re: filterM function (Alexey Shmalko)
3. integer to noise function in Haskell (Florian Gillard)
4. Re: Return function (Kim-Ee Yeoh)
5. Re: integer to noise function in Haskell (Florian Gillard)
6. Re: integer to noise function in Haskell (Kim-Ee Yeoh)
----------------------------------------------------------------------
Message: 1
Date: Wed, 22 Apr 2015 14:08:45 +0200
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] filterM function
Message-ID:
<capmptcux7moeck_mk7akiaj7ybrcazgixqa3qno-bi--xhp...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
You need to understand:
1) do-notation
2) Monad instance for List
1) do-notation is just syntactic sugar around (>>=) Monad operator. So:
filterM p (x:xs) = p x >>= \flg -> filterM p xs >>= \ys -> if flg then
x:ys else ys
2) Monad instance for List:
http://hackage.haskell.org/package/base-4.8.0.0/docs/src/GHC-Base.html#line-726
In particular: xs >>= f = [y | x <- xs, y <- f x]
or the equivalent: xs >>= f = concatMap f xs
-- filterM specialized for []
filterM :: (a -> [Bool]) -> [a] -> [[a]]
filterM _ [] = [[]]
filterM p (x:xs) = concatMap (\flg -> concatMap (\ys -> [if flg then
x:ys else ys]) (filterM p xs)) (p x)
powerset :: [a] -> [[a]]
powerset xs = filterM (\x -> [True, False]) xs
i.e.
powerset [] = [[]]
powerset (x:xs) = concatMap (\flg -> concatMap (\ys -> [if flg then
x:ys else ys]) (powerset xs)) [True,False]
i.e.
powerset [] = [[]]
powerset (x:xs) = concatMap (\flg -> fmap (\ys -> if flg then x:ys
else ys) (powerset xs)) [True,False]
i.e.
powerset [] = [[]]
powerset (x:xs) = let ys = powerset xs in fmap (x:) ys ++ ys
I would directly use the latter form instead of using filterM to
implement powerset.
Sylvain
2015-04-22 11:22 GMT+02:00 Shishir Srivastava <[email protected]>:
> Hi Mike,
>
> Thanks for your response. I was aware that 'casting' doesn't really fit in
> Haskell vocab but for the lack of better word used it.
>
> My question was however more towards the usage of [Bool] in the 'if'
> statement of the filterM function.
>
> More precisely - How does 'if [True, False] then x else y' work , because
> when I do this in GHCi it throws up the following error ?
>
>>>Couldn't match expected type `Bool' with actual type `[Bool]'
>
> Clearly the 'if' construct does not take a list of Boolean but a single
> Boolean value so how does filterM use it in it's implementation.
>
> Hope have made myself clear this time.
>
> Thanks,
> Shishir
>
>
>>
>> From: Mike Meyer <[email protected]>
>> To: The Haskell-Beginners Mailing List - Discussion of primarily
>> beginner-level topics related to Haskell <[email protected]>
>> Cc:
>> Date: Wed, 22 Apr 2015 04:15:31 -0500
>> Subject: Re: [Haskell-beginners] filterM function
>>
>> On Wed, Apr 22, 2015 at 3:31 AM, Shishir Srivastava
>> <[email protected]> wrote:
>>>
>>> I still don't quite understand how 'flg' being a boolean [] is used in
>>> the last 'if statement' of implementation because when I try to do the same
>>> thing outside in GHCi it fails miserably even though I am casting it to
>>> [Int] -
>>>
>>> --
>>> return (if [True, False] then "4" else "3")::[Int]
>>
>>
>> "cast" is a misnomer in Haskell. When you add a type to an expression, you
>> aren't changing the type of the expression like a C-style cast, but picking
>> out a type from the set of possible types for that expression.
>>
>> Ignoring the if part and and focusing on return, which has a type Monad m
>> => a -> m a. [Int] is equivalent to [] Int, so [] would be the Monad, and a
>> is Int. While 3 can be an Int, "3", can't. So you could do return 3 :: [Int]
>> or equivalently return 3 :: [] Int to get [3], you can't do return "3" ::
>> [Int], because "3" can't be an Int. You can do return "3" :: [String], since
>> "3" is a string. Just to show the range of possibilities, you can do return
>> 3 :: IO Float, and get back 3.0 as an IO action. The monad in the type is
>> IO, and 3 can be interpreted as a Float.
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
------------------------------
Message: 2
Date: Wed, 22 Apr 2015 15:22:35 +0300
From: Alexey Shmalko <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] filterM function
Message-ID:
<cafc2pc53ztnipzse7uxkcbawd3o1yr-ou0owshzu4qvsyxr...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
I see misunderstand comes from
flg <- p x
In fact in the following code flg has type Bool:
flg <- [True, False]
For lists, >>= (and therefore <-) assigns every value out of list to
flg and tries to proceed. The final `return` returns only a single
element out of result, and all the results are concatenated together.
On Wed, Apr 22, 2015 at 12:22 PM, Shishir Srivastava
<[email protected]> wrote:
> Hi Mike,
>
> Thanks for your response. I was aware that 'casting' doesn't really fit in
> Haskell vocab but for the lack of better word used it.
>
> My question was however more towards the usage of [Bool] in the 'if'
> statement of the filterM function.
>
> More precisely - How does 'if [True, False] then x else y' work , because
> when I do this in GHCi it throws up the following error ?
>
>>>Couldn't match expected type `Bool' with actual type `[Bool]'
>
> Clearly the 'if' construct does not take a list of Boolean but a single
> Boolean value so how does filterM use it in it's implementation.
>
> Hope have made myself clear this time.
>
> Thanks,
> Shishir
>
>
>>
>> From: Mike Meyer <[email protected]>
>> To: The Haskell-Beginners Mailing List - Discussion of primarily
>> beginner-level topics related to Haskell <[email protected]>
>> Cc:
>> Date: Wed, 22 Apr 2015 04:15:31 -0500
>> Subject: Re: [Haskell-beginners] filterM function
>>
>> On Wed, Apr 22, 2015 at 3:31 AM, Shishir Srivastava
>> <[email protected]> wrote:
>>>
>>> I still don't quite understand how 'flg' being a boolean [] is used in
>>> the last 'if statement' of implementation because when I try to do the same
>>> thing outside in GHCi it fails miserably even though I am casting it to
>>> [Int] -
>>>
>>> --
>>> return (if [True, False] then "4" else "3")::[Int]
>>
>>
>> "cast" is a misnomer in Haskell. When you add a type to an expression, you
>> aren't changing the type of the expression like a C-style cast, but picking
>> out a type from the set of possible types for that expression.
>>
>> Ignoring the if part and and focusing on return, which has a type Monad m
>> => a -> m a. [Int] is equivalent to [] Int, so [] would be the Monad, and a
>> is Int. While 3 can be an Int, "3", can't. So you could do return 3 :: [Int]
>> or equivalently return 3 :: [] Int to get [3], you can't do return "3" ::
>> [Int], because "3" can't be an Int. You can do return "3" :: [String], since
>> "3" is a string. Just to show the range of possibilities, you can do return
>> 3 :: IO Float, and get back 3.0 as an IO action. The monad in the type is
>> IO, and 3 can be interpreted as a Float.
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
------------------------------
Message: 3
Date: Wed, 22 Apr 2015 15:39:47 +0200
From: Florian Gillard <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] integer to noise function in Haskell
Message-ID:
<CAGLpzLXBEoc1yD11MCYfU0QMdj1-YEiK_MQ5vZ=1n0rzmm+...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
I am trying to implement a basic pelin noise function, but I have some
problem with the integer to noise function used to generate deterministic
noise from integer inputs.
the function I am trying to implement is defined there:
http://libnoise.sourceforge.net/noisegen/index.html#continuousnoise
and my code so far look like this:
noise2d :: (Int32, Int32) -> Double
noise2d (x, y) =
let m = x + y * 57
n = (shiftR m 13) ^ m
j = (n * (n * n * 15731 + 789221) + 1376312589) .&. 0x7fffffff
in 1.0 - (fromIntegral j / 1073741824.0)
the code compile but I get the same result for any input, due to the fact
that n is evaluated to 0.
Is there a better way to do that?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150422/8640f79d/attachment-0001.html>
------------------------------
Message: 4
Date: Wed, 22 Apr 2015 21:00:39 +0700
From: Kim-Ee Yeoh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Return function
Message-ID:
<capy+zdqjhofc4tufyycomj5qn57mmo4ofgkob3q2m-_yh7+...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
> My guess is that you are running it in GHCi, which means that
> it gets coerced to the IO monad.
>
"Coercion" doesn't quite capture it. Also it happens that the haskell
research literature uses it in a technically specific context that doesn't
apply here.
What's going on in
return (Just 3)
being _executed_ in ghci to produce
Just 3
is the REPL's defaulting to IO kicking in.
p.s. In the above, I've made distinct execution from evaluation. This helps
folks navigate the beginner-hazardous terrain of overloaded English.
-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150422/24a3a159/attachment-0001.html>
------------------------------
Message: 5
Date: Wed, 22 Apr 2015 16:07:30 +0200
From: Florian Gillard <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] integer to noise function in Haskell
Message-ID:
<CAGLpzLV7S-t7Oy4JZMF6PtOnAE03GpKX29WQr=PwJu_vutzc=w...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Got it, the error was coming from me thinking of the C "^" operator as
exponent, instead of bitwise xoring.
On Wed, Apr 22, 2015 at 3:39 PM, Florian Gillard <[email protected]>
wrote:
> I am trying to implement a basic pelin noise function, but I have some
> problem with the integer to noise function used to generate deterministic
> noise from integer inputs.
>
> the function I am trying to implement is defined there:
> http://libnoise.sourceforge.net/noisegen/index.html#continuousnoise
>
> and my code so far look like this:
>
> noise2d :: (Int32, Int32) -> Double
> noise2d (x, y) =
> let m = x + y * 57
> n = (shiftR m 13) ^ m
> j = (n * (n * n * 15731 + 789221) + 1376312589) .&. 0x7fffffff
> in 1.0 - (fromIntegral j / 1073741824.0)
>
> the code compile but I get the same result for any input, due to the fact
> that n is evaluated to 0.
>
> Is there a better way to do that?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150422/3e01a7e9/attachment-0001.html>
------------------------------
Message: 6
Date: Wed, 22 Apr 2015 21:13:05 +0700
From: Kim-Ee Yeoh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] integer to noise function in Haskell
Message-ID:
<CAPY+ZdTODmvBzTahfE2Aoq=fkztj41yzseusq2hffqxawzo...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Wed, Apr 22, 2015 at 8:39 PM, Florian Gillard <[email protected]>
wrote:
> I am trying to implement a basic pelin noise function, but I have some
> problem with the integer to noise function used to generate deterministic
> noise from integer inputs.
>
> the function I am trying to implement is defined there:
> http://libnoise.sourceforge.net/noisegen/index.html#continuousnoise
I understand you're trying to transcribe an algorithm given in C into
Haskell.
That's a great way to learn. In fact, if you zoom out a bit, there's more
to explore in the various prng libraries on hackage.
For it so happens that the exact function you cited is a prng, one of many.
-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150422/7646d804/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 82, Issue 29
*****************************************