Send Beginners mailing list submissions to beginners@haskell.org 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 beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Question on fromIntegral (Jonathan Drews) 2. Re: Question on fromIntegral (David McBride) 3. Re: Question on fromIntegral (Jonathan Drews) ---------------------------------------------------------------------- Message: 1 Date: Wed, 30 Nov 2022 17:41:03 -0700 From: Jonathan Drews <jondr...@fastmail.com> To: beginners@haskell.org Subject: [Haskell-beginners] Question on fromIntegral Message-ID: <y4f4h6025zvta...@leo.my.domain> Content-Type: text/plain; charset=us-ascii Hi Folks: I am using Glasgow Haskell Compilation System, version 9.2.4 on OpenBSD 7.2. I am stumped by the following problem. If I run the following program in ghci, then it works fine. Look: fact :: Integer -> Integer fact n = product [1..n] term :: Double -> Double term x = x**4/fromIntegral(fact(4)) $ ghci GHCi, version 9.2.4: https://www.haskell.org/ghc/ :? for help ghci> :l seriesTermTest.hs [1 of 1] Compiling Main ( seriesTermTest.hs, interpreted ) Ok, one module loaded. ghci> term 1.0 4.1666666666666664e-2 However if I use fromIntegral inside a list comprehension like so: fact :: Integer -> Integer fact n = product [1..n] expon :: Double -> Double expon x = sum [x**i/fromIntegral(fact(i)) | i <- [0..50]] then I get the following error message: $ ghci GHCi, version 9.2.4: https://www.haskell.org/ghc/ :? for help ghci> :l ePowerSeries.hs [1 of 1] Compiling Main ( ePowerSeries.hs, interpreted ) ePowerSeries.hs:6:40: error: * Couldn't match expected type `Integer' with actual type `Double' * In the first argument of `fact', namely `(i)' In the first argument of `fromIntegral', namely `(fact (i))' In the second argument of `(/)', namely `fromIntegral (fact (i))' | 6 | expon x = sum [x**i/fromIntegral(fact(i)) | i <- [0..50]] | ^ Failed, no modules loaded. What am I doing wrong? -- Kind regards, Jonathan ------------------------------ Message: 2 Date: Wed, 30 Nov 2022 23:05:21 -0500 From: David McBride <toa...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Question on fromIntegral Message-ID: <can+tr41zqefn0u9mabcyxbcdpoypqq+okdqnlr3et2xhwho...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" It is because (**) takes floating point numbers. In your first equation both 4s are different. The second 4 is an Integer but the first is a some Floating type aka 4.0. In your second equation, both of those numbers are constrained to whatever type i is, and fact demands an Integer and (**) demands a Float or Double or some other Floating type, which cannot be reconciled. You should be able to fix this with (untested) x**fromIntegral(i), which converts i from an Integer to Num and all Floating are Nums. On Wed, Nov 30, 2022, 19:41 Jonathan Drews <jondr...@fastmail.com> wrote: > Hi Folks: > > I am using Glasgow Haskell Compilation System, version 9.2.4 on > OpenBSD 7.2. I am stumped by the following problem. If I run the > following program in ghci, then it works fine. Look: > > fact :: Integer -> Integer > fact n = product [1..n] > > > term :: Double -> Double > term x = x**4/fromIntegral(fact(4)) > > $ ghci > GHCi, version 9.2.4: https://www.haskell.org/ghc/ :? for help > ghci> :l seriesTermTest.hs > [1 of 1] Compiling Main ( seriesTermTest.hs, interpreted ) > Ok, one module loaded. > ghci> term 1.0 > 4.1666666666666664e-2 > > However if I use fromIntegral inside a list comprehension like so: > > fact :: Integer -> Integer > fact n = product [1..n] > > > expon :: Double -> Double > expon x = sum [x**i/fromIntegral(fact(i)) | i <- [0..50]] > > then I get the following error message: > > $ ghci > GHCi, version 9.2.4: https://www.haskell.org/ghc/ :? for help > ghci> :l ePowerSeries.hs > [1 of 1] Compiling Main ( ePowerSeries.hs, interpreted ) > > ePowerSeries.hs:6:40: error: > * Couldn't match expected type `Integer' with actual type `Double' > * In the first argument of `fact', namely `(i)' > In the first argument of `fromIntegral', namely `(fact (i))' > In the second argument of `(/)', namely `fromIntegral (fact > (i))' > | > 6 | expon x = sum [x**i/fromIntegral(fact(i)) | i <- [0..50]] > | ^ > Failed, no modules loaded. > > What am I doing wrong? > > -- > Kind regards, > Jonathan > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20221130/bf05e787/attachment-0001.html> ------------------------------ Message: 3 Date: Wed, 30 Nov 2022 22:17:49 -0700 From: Jonathan Drews <jondr...@fastmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Question on fromIntegral Message-ID: <Y4g4/ewffhxio...@leo.my.domain> Content-Type: text/plain; charset=us-ascii On Wed, Nov 30, 2022 at 11:05:21PM -0500, David McBride wrote: > It is because (**) takes floating point numbers. In your first equation > both 4s are different. The second 4 is an Integer but the first is a some > Floating type aka 4.0. Thank you David: I switched from x**1 to x^i and it worked fact :: Integer -> Integer fact n = product [1..n] expon :: Double -> Double expon x = sum [x^i/fromIntegral(fact(i)) | i <- [0..50]] $ ghci GHCi, version 9.2.4: https://www.haskell.org/ghc/ :? for help ghci> :l ePowerSeries.hs [1 of 1] Compiling Main ( ePowerSeries.hs, interpreted ) Ok, one module loaded. ghci> exp exp expon exponent ghci> expon 2 7.389056098930649 ghci> exp 2 7.38905609893065 I believe x^i has a similar effect to x**fromIntegral(i) -- Kind regards, Jonathan ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 167, Issue 1 *****************************************