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.  how to print a floating-point number? (Sergei Winitzki)
   2. Re:  how to print a floating-point number? (Adrian Neumann)
   3. Re:  how to print a floating-point number? (Daniel Fischer)
   4. Re:  how to print a floating-point number? (Sergei Winitzki)
   5.  First request for references (Alan Cameron)
   6. Re:  First request for references (Thomas Davie)
   7. Re:  First request for references (Brent Yorgey)
   8.  typeOf raises Error (Frank Schwidom)


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

Message: 1
Date: Thu, 8 Jan 2009 17:45:09 +0100
From: "Sergei Winitzki" <[email protected]>
Subject: [Haskell-beginners] how to print a floating-point number?
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Subject: how to print a floating-point number?
hi,

I am very new to Haskell. I am trying to benchmark the CPU time needed
for a computation, and I can't figure out how to print a
floating-point number.

My code is as follows, and I expected it to work:

import System.CPUTime
main = do
       let result = some_computation
       print result
       time <- getCPUTime          -- this is an Integer that needs
to be divided by 1e12 to get time in seconds
       print (time / 1.0e12)           -- I want this to print a
floating-point number


But this does not compile.
Error message:    No instance for (Fractional Integer)
     arising from use of `/' at fact1.hs:18:15-28
   Possible fix: add an instance declaration for (Fractional Integer)
   In the first argument of `print', namely `(time1 / 1.0e12)'

 I thought this should work because e.g. 12 / 7 evaluates to
1.7142857142857142 in ghci.
I understand this is some problem with types, but surely it is fixed
not by adding any instance declarations but perhaps by using some
Prelude or Numeric function. But which one?
 I tried everything I could find in the documentation: showFloat,
adding ::Float everywhere, adding fromIntegral, etc.etc. - nothing
works. All the books and the tutorials I looked at seem to discuss at
length such nice things as Fibonacci numbers and recursive factorial
functions rather than a practical problem like this.

help will be much appreciated!

Sergei


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

Message: 2
Date: Thu, 8 Jan 2009 19:22:34 +0100
From: Adrian Neumann <[email protected]>
Subject: Re: [Haskell-beginners] how to print a floating-point number?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

Unlike other languages Haskell doesn't automatically convert  
numbertypes.

getCPUTime returns an Integer, which can't be divided with / (you'd  
have to use "div" for integer-division). However you can explicitly  
convert an Integer (or any Integral type, Ints too) with "fromIntegral".

>        print (fromIntegral time / 1.0e12)

should work. For more information refer to the haskell-wiki

 > http://haskell.org/haskellwiki/Converting_numbers


Am 08.01.2009 um 17:45 schrieb Sergei Winitzki:

> Subject: how to print a floating-point number?
> hi,
>
> I am very new to Haskell. I am trying to benchmark the CPU time needed
> for a computation, and I can't figure out how to print a
> floating-point number.
>
> My code is as follows, and I expected it to work:
>
> import System.CPUTime
> main = do
>        let result = some_computation
>        print result
>        time <- getCPUTime          -- this is an Integer that needs
> to be divided by 1e12 to get time in seconds
>        print (time / 1.0e12)           -- I want this to print a
> floating-point number
>
>
> But this does not compile.
> Error message:    No instance for (Fractional Integer)
>      arising from use of `/' at fact1.hs:18:15-28
>    Possible fix: add an instance declaration for (Fractional Integer)
>    In the first argument of `print', namely `(time1 / 1.0e12)'
>
>  I thought this should work because e.g. 12 / 7 evaluates to
> 1.7142857142857142 in ghci.
> I understand this is some problem with types, but surely it is fixed
> not by adding any instance declarations but perhaps by using some
> Prelude or Numeric function. But which one?
>  I tried everything I could find in the documentation: showFloat,
> adding ::Float everywhere, adding fromIntegral, etc.etc. - nothing
> works. All the books and the tutorials I looked at seem to discuss at
> length such nice things as Fibonacci numbers and recursive factorial
> functions rather than a practical problem like this.
>
> help will be much appreciated!
>
> Sergei
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 194 bytes
Desc: Signierter Teil der Nachricht
Url : 
http://www.haskell.org/pipermail/beginners/attachments/20090108/ac5423b9/PGP-0001.bin

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

Message: 3
Date: Thu, 8 Jan 2009 19:28:59 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] how to print a floating-point number?
To: "Sergei Winitzki" <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Donnerstag, 8. Januar 2009 17:45 schrieb Sergei Winitzki:
> Subject: how to print a floating-point number?
> hi,
>
> I am very new to Haskell. I am trying to benchmark the CPU time needed
> for a computation, and I can't figure out how to print a
> floating-point number.
>
> My code is as follows, and I expected it to work:
>
> import System.CPUTime
> main = do
>        let result = some_computation
>        print result
>        time <- getCPUTime          -- this is an Integer that needs
> to be divided by 1e12 to get time in seconds
>        print (time / 1.0e12)           -- I want this to print a
> floating-point number
>

You have to convert the Integer to a floating point number first, use

fromInteger
or
fromIntegral

for that. Haskell does not do automatic conversion between numeric types.

>
> But this does not compile.
> Error message:    No instance for (Fractional Integer)
>      arising from use of `/' at fact1.hs:18:15-28
>    Possible fix: add an instance declaration for (Fractional Integer)
>    In the first argument of `print', namely `(time1 / 1.0e12)'
>
>  I thought this should work because e.g. 12 / 7 evaluates to
> 1.7142857142857142 in ghci.

That is because numeric *literals* are polymorphic, as they are parsed as e.g. 
"fromInteger 12" if it's an integer literal or "fromRational 3.24" if it's a 
non-integer literal.

> I understand this is some problem with types, but surely it is fixed
> not by adding any instance declarations but perhaps by using some
> Prelude or Numeric function. But which one?
>  I tried everything I could find in the documentation: showFloat,
> adding ::Float everywhere, adding fromIntegral, etc.etc. - nothing
> works. All the books and the tutorials I looked at seem to discuss at
> length such nice things as Fibonacci numbers and recursive factorial
> functions rather than a practical problem like this.

When you are looking for a function, it's a good idea to ask hoogle 
(http://haskell.org/hoogle/) for a function of appropriate type. Asking for a 
function Integer -> Double, the abovementioned are results 1 and 3.
>
> help will be much appreciated!
>
> Sergei

HTH,
Daniel



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

Message: 4
Date: Thu, 8 Jan 2009 22:15:39 +0100
From: "Sergei Winitzki" <[email protected]>
Subject: Re: [Haskell-beginners] how to print a floating-point number?
To: "Daniel Fischer" <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Great, it worked! I was trying to convert 1.0e12 to float, thinking
that the integer variable will be coerced automatically to float. The
correct code is

print ( (fromInteger time) / 1.0e12 )

thanks for the explanations!

On Thu, Jan 8, 2009 at 7:28 PM, Daniel Fischer <[email protected]> wrote:
> Am Donnerstag, 8. Januar 2009 17:45 schrieb Sergei Winitzki:
>> Subject: how to print a floating-point number?
>> hi,
>>
>> I am very new to Haskell. I am trying to benchmark the CPU time needed
>> for a computation, and I can't figure out how to print a
>> floating-point number.
>>
>> My code is as follows, and I expected it to work:
>>
>> import System.CPUTime
>> main = do
>>        let result = some_computation
>>        print result
>>        time <- getCPUTime          -- this is an Integer that needs
>> to be divided by 1e12 to get time in seconds
>>        print (time / 1.0e12)           -- I want this to print a
>> floating-point number
>>
>
> You have to convert the Integer to a floating point number first, use
>
> fromInteger
> or
> fromIntegral
>
> for that. Haskell does not do automatic conversion between numeric types.
>
>>
>> But this does not compile.
>> Error message:    No instance for (Fractional Integer)
>>      arising from use of `/' at fact1.hs:18:15-28
>>    Possible fix: add an instance declaration for (Fractional Integer)
>>    In the first argument of `print', namely `(time1 / 1.0e12)'
>>
>>  I thought this should work because e.g. 12 / 7 evaluates to
>> 1.7142857142857142 in ghci.
>
> That is because numeric *literals* are polymorphic, as they are parsed as e.g.
> "fromInteger 12" if it's an integer literal or "fromRational 3.24" if it's a
> non-integer literal.
>
>> I understand this is some problem with types, but surely it is fixed
>> not by adding any instance declarations but perhaps by using some
>> Prelude or Numeric function. But which one?
>>  I tried everything I could find in the documentation: showFloat,
>> adding ::Float everywhere, adding fromIntegral, etc.etc. - nothing
>> works. All the books and the tutorials I looked at seem to discuss at
>> length such nice things as Fibonacci numbers and recursive factorial
>> functions rather than a practical problem like this.
>
> When you are looking for a function, it's a good idea to ask hoogle
> (http://haskell.org/hoogle/) for a function of appropriate type. Asking for a
> function Integer -> Double, the abovementioned are results 1 and 3.
>>
>> help will be much appreciated!
>>
>> Sergei
>
> HTH,
> Daniel
>
>


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

Message: 5
Date: Sat, 10 Jan 2009 20:36:14 -0000
From: "Alan Cameron" <[email protected]>
Subject: [Haskell-beginners] First request for references
To: <[email protected]>
Message-ID: <c23b3305956948218bd5b7356acee...@alanxps>
Content-Type: text/plain;       charset="us-ascii"

Hi,

I stumbled across Haskell while researching 'functional programming'.
I have done some initial reading and have the following questions:

1. Are there any references to real-world applications using this language?

I ask because most of the articles I have found seem to be of an extreme
academic nature. I want to be sure that my time will not be wasted delving
deeper.

2. Are there any tutorials with full code included which I can use on the
HUGS system I have installed?

I have found a few "A Gentle Introduction to Haskell 98" which assumes far
too much previous knowledge to be immediately useful to me.

Thanks for any replies.

Alan Cameron



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

Message: 6
Date: Sat, 10 Jan 2009 21:47:37 +0100
From: Thomas Davie <[email protected]>
Subject: Re: [Haskell-beginners] First request for references
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=WINDOWS-1252; format=flowed;
        delsp=yes


On 10 Jan 2009, at 21:36, Alan Cameron wrote:

> Hi,
>
> I stumbled across Haskell while researching 'functional programming'.
> I have done some initial reading and have the following questions:
>
> 1. Are there any references to real-world applications using this  
> language?
>
> I ask because most of the articles I have found seem to be of an  
> extreme
> academic nature. I want to be sure that my time will not be wasted  
> delving
> deeper.

Here's a few real world apps written in Haskell:
darcs
Frag
XMonad
pugs (perl 6 interpretter)
ghc
and many more

If you want to learn about writing real world programs, try the new  
book – Real World Haskell.

> 2. Are there any tutorials with full code included which I can use  
> on the
> HUGS system I have installed?
>
> I have found a few "A Gentle Introduction to Haskell 98" which  
> assumes far
> too much previous knowledge to be immediately useful to me.

The learning haskell page on the wiki has a *lot* of references to  
lots of different tutorials and books.

Bob

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

Message: 7
Date: Sat, 10 Jan 2009 15:51:01 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] First request for references
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Sat, Jan 10, 2009 at 08:36:14PM -0000, Alan Cameron wrote:
> Hi,
> 
> I stumbled across Haskell while researching 'functional programming'.
> I have done some initial reading and have the following questions:
> 
> 1. Are there any references to real-world applications using this language?
> 
> I ask because most of the articles I have found seem to be of an extreme
> academic nature. I want to be sure that my time will not be wasted delving
> deeper.
> 
> 2. Are there any tutorials with full code included which I can use on the
> HUGS system I have installed?
> 
> I have found a few "A Gentle Introduction to Haskell 98" which assumes far
> too much previous knowledge to be immediately useful to me.
> 
> Thanks for any replies.
> 
> Alan Cameron

Hi Alan, welcome!

You should check out 'Real World Haskell', recently published by
O'Reilly. It sounds like exactly what you are looking for. You can
read it for free online:

  http://www.realworldhaskell.org/blog/

and buy a dead tree copy if you like it.

Feel free to direct more questions towards this list, or come ask
questions on IRC, in the #haskell channel on irc.freenode.net.

-Brent


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

Message: 8
Date: Sun, 11 Jan 2009 10:27:14 +0000
From: Frank Schwidom <[email protected]>
Subject: [Haskell-beginners] typeOf raises Error
To: [email protected]
Message-ID: <20090111102713.ga4...@bigbox>
Content-Type: text/plain; charset=us-ascii

Hi,

if im calling (on the console)

"typeOf 1"

after importing Data.Typeable, i will gain 

"Integer", as i expected, but if i do so in code which is 
to compile, then there raises an error

------------
import Data.Typeable

main=
 print (typeOf 1)
------------

$ ghc -c DT.hs 

$ ghc -c DT.hs 

DT.hs:4:15:
    Ambiguous type variable `t' in the constraints:
      `Num t' arising from the literal `1' at DT.hs:4:15
      `Typeable t' arising from a use of `typeOf' at DT.hs:4:8-15
    Probable fix: add a type signature that fixes these type variable(s)

what can i do, to solve this problem?

Regards



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

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


End of Beginners Digest, Vol 7, Issue 8
***************************************

Reply via email to