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. Lazy state + IO not lazy? (Jan Snajder)
2. Haskell typing question (Cui Liqiang)
3. Re: Haskell typing question (Brandon Allbery)
4. Re: Lazy state + IO not lazy? (Kim-Ee Yeoh)
----------------------------------------------------------------------
Message: 1
Date: Sun, 26 Oct 2014 22:34:44 +0100
From: Jan Snajder <[email protected]>
To: beginners <[email protected]>
Subject: [Haskell-beginners] Lazy state + IO not lazy?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
Dear Haskellers,
I'm confused with the behaviour of a lazy state monad when combined with
an IO monad. Look at this:
ghci> Control.Monad.State.Lazy.evalState (head `fmap` mapM return
(1:undefined)) 0
1
ghci> Control.Monad.State.Lazy.evalStateT (head `fmap` mapM return
(1:undefined)) 0
*** Exception: Prelude.undefined
I would have expected the second case to behave identically to the first
case. Why is this not the case?
Cheers,
Jan
------------------------------
Message: 2
Date: Mon, 27 Oct 2014 06:52:08 +0800
From: Cui Liqiang <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] Haskell typing question
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Hi,
I am doing an exercise in Haskell, which is converting a string like ?$123.312?
to double value. Below is my code:
module Main where
import Data.Char
caluInt l = foldl1 (\acc x -> acc * 10 + x) (map digitToInt l)
caluDecimal l = (foldr1 (\x acc -> acc / 10.0 + x) (map digitToInt l))
convert(x:xs) =
let num = [e | e <- xs, e /= ',']
intPart = takeWhile (/='.') num
decimalPart = tail(dropWhile (/='.') num)
in (caluInt intPart) + (caluDecimal decimalPart)
And I got an error in this line: caluDecimal l = (foldr1 (\x acc -> acc /
10.0 + x) (map digitToInt l)),
which says:
No instance for (Fractional Int) arising from a use of `/'
Possible fix: add an instance declaration for (Fractional Int)
In the first argument of `(+)', namely `acc / 10.0'
In the expression: acc / 10.0 + x
In the first argument of `foldr1', namely
`(\ x acc -> acc / 10.0 + x)'
Why Haskell insists that 10.0 is a Int? How can I explicitly tell Haskell I
want a Fractional?
--
Cui Liqiang
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20141027/8e0bb3a0/attachment-0001.html>
------------------------------
Message: 3
Date: Sun, 26 Oct 2014 18:51:16 -0400
From: Brandon Allbery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Haskell typing question
Message-ID:
<CAKFCL4XfhZSqi=tpCbN29D5aX=h6okicpgys6uru1nkanjl...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Sun, Oct 26, 2014 at 6:52 PM, Cui Liqiang <[email protected]> wrote:
> Why Haskell insists that 10.0 is a Int? How can I explicitly tell Haskell
> I want a Fractional?
>
Because digitToInt means exactly what it says. If you want it to become
something other than Int, apply fromIntegral to its result.
--
brandon s allbery kf8nh sine nomine associates
[email protected] [email protected]
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20141026/898c66cd/attachment-0001.html>
------------------------------
Message: 4
Date: Mon, 27 Oct 2014 13:08:09 +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] Lazy state + IO not lazy?
Message-ID:
<capy+zdr9ea+rny7qcwsagb2yb3n5fcjqqegnbf7zzbui72d...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
Better to drill into the heart of the question and put aside state
transformers for now.
Consider
let u = return () :: IO () in
expr >> u
where expr ranges over (return undefined), (undefined), and (error "urk").
Are their executions surprising?
Compare to
let u = return () :: State Int () in
evalState (expr >> u) 0
for both strict and lazy state. IO above matches one of them. Suppose
IO's behavior matches the other, what happens?
Now with the definition of mapM in mind, consider the difference
between your original:
Lazy.evalStateT (head `fmap` mapM return (1:undefined)) 0
and
Lazy.evalStateT (head `fmap` mapM return [1,undefined]) 0
Did you mean the latter?
-- Kim-Ee
On 10/27/14, Jan Snajder <[email protected]> wrote:
> Dear Haskellers,
>
> I'm confused with the behaviour of a lazy state monad when combined with
> an IO monad. Look at this:
>
> ghci> Control.Monad.State.Lazy.evalState (head `fmap` mapM return
> (1:undefined)) 0
> 1
>
> ghci> Control.Monad.State.Lazy.evalStateT (head `fmap` mapM return
> (1:undefined)) 0
> *** Exception: Prelude.undefined
>
> I would have expected the second case to behave identically to the first
> case. Why is this not the case?
>
> Cheers,
> Jan
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
-- Kim-Ee
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 76, Issue 25
*****************************************