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: Tutorial/Book with Exercises (Jason White)
   2.  Re: question about styles of recursion (7stud)
   3.  having trouble compiling (7stud)
   4.  Re: having trouble compiling (7stud)
   5.  beginner's type error (Ivan Moore)
   6. Re:  Re: Tutorial/Book with Exercises (Michael Mossey)
   7. Re:  beginner's type error (Peter Verswyvelen)
   8. Re:  Re: question about styles of recursion (Brent Yorgey)


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

Message: 1
Date: Fri, 27 Mar 2009 09:00:16 +0000 (UTC)
From: Jason White <[email protected]>
Subject: [Haskell-beginners] Re: Tutorial/Book with Exercises
To: [email protected]
Message-ID: <[email protected]>

Michael Mossey  <[email protected]> wrote:

>YAHT has some hard exercises, early on. He introduces continuations in 
>chapter 4, briefly, and then casually asks you to rewrite map and filter 
>  in continuation-passing-style. I was stumped. 

So am I.

Do you have any hints for these, without giving the answers away (I know the
answers are in the appendix if I really need them)?




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

Message: 2
Date: Fri, 27 Mar 2009 09:12:46 +0000 (UTC)
From: 7stud <[email protected]>
Subject: [Haskell-beginners] Re: question about styles of recursion
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Daniel Fischer <daniel.is.fischer <at> web.de> writes:
>
> (/) in the first equation is at type Double -> Double -> Double.
>

Then why don't I get an error here:

Prelude> 2 / 4
0.5
Prelude> 2 / fromIntegral 4
0.5

And why does this happen:

Prelude> let x = 2
Prelude> :type x
x :: Integer
Prelude> x / fromIntegral 4

<interactive>:1:0:
    No instance for (Fractional Integer)
      arising from a use of `/' at <interactive>:1:0-17
    Possible fix: add an instance declaration for (Fractional Integer)
    In the expression: x / fromIntegral 4
    In the definition of `it': it = x / fromIntegral 4


And how do I read this type:

Prelude> :type fromIntegral
fromIntegral :: (Num b, Integral a) => a -> b

What does the => mean?





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

Message: 3
Date: Fri, 27 Mar 2009 09:51:23 +0000 (UTC)
From: 7stud <[email protected]>
Subject: [Haskell-beginners] having trouble compiling
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Here is the error I am getting (I always get the first warning so that is 
nothing
new):

$ ghc -o Main.hs SimpleJSON.hs PrettyJSON.hs Prettify.hs
compilation IS NOT required
compilation IS NOT required
/usr/libexec/gcc/i686-apple-darwin8/4.0.1/ld: warning -F: directory name 
(/Users/me/Library/Frameworks) does not exist

/usr/libexec/gcc/i686-apple-darwin8/4.0.1/ld: Undefined symbols:
_ZCMain_main_closure
___stginit_ZCMain
collect2: ld returned 1 exit status


Here are my files:

Main.hs
-----
module Main () where

import SimpleJSON 
import PrettyJSON
import Prettify

main = let s = JString "hello world" 
           aDoc = renderJValue s
       in printPrettified aDoc



SimpleJSON.hs
--------

module SimpleJSON
    (
     JValue(..)
    ) where 

data JValue = JNumber Double
            | JString String
            | JArray [JValue]
            | JObject [(String, JValue)]
            | JBool Bool
            | JNull 
              deriving (Eq, Ord, Show)



PrettyJSON.hs:
-------

module PrettyJSON where

import SimpleJSON 
import Prettify 

renderJValue :: JValue -> Doc
renderJValue (JNumber f)    = DNumber f
renderJValue (JString s)    = DText s 
renderJValue (JBool True)   = DBool True
renderJValue (JBool False)  = DBool False
renderJValue JNull          = DNull 



Prettify.hs:
------
module Prettify where

data Doc = DNumber Double
         | DText String
         | DBool Bool
         | DNull 
           deriving (Show)
         

getPrettyOutput :: Doc -> String
getPrettyOutput (DNumber d)     = "double: " ++ show d
getPrettyOutput (DText s)       = "string: " ++ show s
getPrettyOutput (DBool True)    = "bool: true"
getPrettyOutput (DBool False)   = "bool: false"
getPrettyOutput DNull           = "null"
         

printPrettfied :: Doc -> IO ()
printPrettified aDoc = putStrLn (getPrettyOutput aDoc)








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

Message: 4
Date: Fri, 27 Mar 2009 10:02:11 +0000 (UTC)
From: 7stud <[email protected]>
Subject: [Haskell-beginners] Re: having trouble compiling
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

7stud <bbxx789_05ss <at> yahoo.com> writes:

> 
> Here is the error I am getting (I always get the first warning so that 
> is nothing new):
> 
> $ ghc -o Main.hs SimpleJSON.hs PrettyJSON.hs Prettify.hs
> compilation IS NOT required
> compilation IS NOT required
> /usr/libexec/gcc/i686-apple-darwin8/4.0.1/ld: warning -F: directory name 
> (/Users/me/Library/Frameworks) does not exist

Whoops.  Forgot the executable name.  I should be doing this:

$ ghc -o simple Main.hs SimpleJSON.hs PrettyJSON.hs Prettify.hs







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

Message: 5
Date: Thu, 26 Mar 2009 22:01:07 +0000
From: Ivan Moore <[email protected]>
Subject: [Haskell-beginners] beginner's type error
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi all,

consider this very small function:

thing n = n + round(sqrt n)

It loads into ghci with no warnings. When I try to run "thing 10" I get:

*Main> :load c:\temp\statictype.hs
[1 of 1] Compiling Main             ( C:\temp\statictype.hs, interpreted )
Ok, modules loaded: Main.
*Main> thing 10

<interactive>:1:0:
    Ambiguous type variable `t' in the constraints:
      `Integral t' arising from a use of `thing' at <interactive>:1:0-7
      `RealFrac t' arising from a use of `thing' at <interactive>:1:0-7
      `Floating t' arising from a use of `thing' at <interactive>:1:0-7
    Probable fix: add a type signature that fixes these type variable(s)

I have tried to add various type signatures (without really knowing
what I'm doing!) and haven't been able to get it to work.

I am confused about a few things related to this:
(a) what type signature fixes it and why it needs any help - it looks
like the sort of thing that type inference shouldn't need any help
with
(b) it looks like a runtime type error and I thought you didn't get
runtime type errors in Haskell
(c) if I substitute 10 for n and do "10 + round(sqrt 10)" I get the
expected answer 13

any help most welcome.

cheers,

Ivan


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

Message: 6
Date: Fri, 27 Mar 2009 05:21:05 -0700
From: Michael Mossey <[email protected]>
Subject: Re: [Haskell-beginners] Re: Tutorial/Book with Exercises
To: Jason White <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed



Jason White wrote:
> Michael Mossey  <[email protected]> wrote:
> 
>> YAHT has some hard exercises, early on. He introduces continuations in 
>> chapter 4, briefly, and then casually asks you to rewrite map and filter 
>>  in continuation-passing-style. I was stumped. 
> 
> So am I.
> 
> Do you have any hints for these, without giving the answers away (I know the
> answers are in the appendix if I really need them)?
> 
Well, I've got some bad news for you. He doesn't include the answers to 
the CPS-related questions. Note: YAHT seems to be incomplete in a number 
of ways, because there are chapter headings that exist only as stubs. 
However, it's a terrific resource, and my thanks to Hal.

I decided to just move on, and I'll come back to continuations when I 
find them in another book.

Mike



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

Message: 7
Date: Fri, 27 Mar 2009 13:29:28 +0100
From: Peter Verswyvelen <[email protected]>
Subject: Re: [Haskell-beginners] beginner's type error
To: Ivan Moore <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

round returns an Integral type, but sqrt expects a Floating type
Prelude> :t sqrt
sqrt :: (Floating a) => a -> a
Prelude> :t round
round :: (RealFrac a, Integral b) => a -> b
Prelude>

Haskell's numeric type classes can be intimidating for beginners, but it
basically means you are combining floating point numbers with integer
numbers, and you must convert these numbers to the same type (just as in C#
or other languages, so that are aware of possible unwanted numerical
effects).

You can use functions like fromIntegral and realToFrac to convert numbers.

So try this

thing n = n + fromIntegral ( round(sqrt n) )

You can also get rid of the parentheses like this:

thing n = n + fromIntegral $ round $ sqrt n


On Thu, Mar 26, 2009 at 11:01 PM, Ivan Moore <[email protected]> wrote:

> Hi all,
>
> consider this very small function:
>
> thing n = n + round(sqrt n)
>
> It loads into ghci with no warnings. When I try to run "thing 10" I get:
>
> *Main> :load c:\temp\statictype.hs
> [1 of 1] Compiling Main             ( C:\temp\statictype.hs, interpreted )
> Ok, modules loaded: Main.
> *Main> thing 10
>
> <interactive>:1:0:
>    Ambiguous type variable `t' in the constraints:
>      `Integral t' arising from a use of `thing' at <interactive>:1:0-7
>      `RealFrac t' arising from a use of `thing' at <interactive>:1:0-7
>      `Floating t' arising from a use of `thing' at <interactive>:1:0-7
>    Probable fix: add a type signature that fixes these type variable(s)
>
> I have tried to add various type signatures (without really knowing
> what I'm doing!) and haven't been able to get it to work.
>
> I am confused about a few things related to this:
> (a) what type signature fixes it and why it needs any help - it looks
> like the sort of thing that type inference shouldn't need any help
> with
> (b) it looks like a runtime type error and I thought you didn't get
> runtime type errors in Haskell
> (c) if I substitute 10 for n and do "10 + round(sqrt 10)" I get the
> expected answer 13
>
> any help most welcome.
>
> cheers,
>
> Ivan
> _______________________________________________
> 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/20090327/b9b9f8aa/attachment-0001.htm

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

Message: 8
Date: Fri, 27 Mar 2009 08:42:56 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Re: question about styles of
        recursion
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Fri, Mar 27, 2009 at 09:12:46AM +0000, 7stud wrote:
> Daniel Fischer <daniel.is.fischer <at> web.de> writes:
> >
> > (/) in the first equation is at type Double -> Double -> Double.
> >
> 
> Then why don't I get an error here:
> 
> Prelude> 2 / 4
> 0.5

There are several things going on here.  The first is that numeric
literals are overloaded; integral numeric literals (like 2) can be of
any numeric type.  What actually happens is that they get wrapped in a
call to fromIntegral.

The other thing going on is ghci's type defaulting: since it doesn't
otherwise know what type this expression should be, it picks Double.
(It would also pick Integer if that worked, but Integer doesn't work
here because of the (/).)

> Prelude> 2 / fromIntegral 4
> 0.5

>From the above discussion you can see that this is exactly the same as
the first expression.

> 
> And why does this happen:
> 
> Prelude> let x = 2
> Prelude> :type x
> x :: Integer

This is because of the defaulting again.  Since there are no
constraints on the type of x this time, it picks Integer.  

> Prelude> x / fromIntegral 4
> 
> <interactive>:1:0:
>     No instance for (Fractional Integer)
>       arising from a use of `/' at <interactive>:1:0-17
>     Possible fix: add an instance declaration for (Fractional Integer)
>     In the expression: x / fromIntegral 4
>     In the definition of `it': it = x / fromIntegral 4
> 

Of course, now that x has type Integer, this is not well-typed; you
can't divide Integers.

> 
> And how do I read this type:
> 
> Prelude> :type fromIntegral
> fromIntegral :: (Num b, Integral a) => a -> b
> 
> What does the => mean?

The => indicates class constraints.  You can read this as
"fromIntegral has type a -> b, as long as b is an instance of the Num
class, and a is an instance of the Integral class."  That is,
fromIntegral can convert a value of any Integral type (Int, Integer)
to a value of any Num type.

-Brent


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

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


End of Beginners Digest, Vol 9, Issue 34
****************************************

Reply via email to