Hi there. I'm a teacher of Maths and am working my way through the Euler Project problems for fun. I have mostly been using Basic, but have read up about Haskell and think it looks like a sensible way to solve many of the problems. OK, so I've downloaded GHCi and am trying to teach myself. So far I have done this: ___ ___ _ / _ \ /\ /\/ __(_) / /_\// /_/ / / | | GHC Interactive, version 6.6.1, for Haskell 98. / /_\\/ __ / /___| | http://www.haskell.org/ghc/ \____/\/ /_/\____/|_| Type :? for help. Loading package base ... linking ... done. Prelude> let f (a,b) = a * floor (100000/b) Prelude> f(2,5) 40000 This function works just as I want it to. Now I try creating a list: Prelude> [(a2+b2,a)| a <- [1..4] , b<- [1..4], a2+b2<20, b<=a] [(2,1),(5,2),(8,2),(10,3),(13,3),(18,3),(17,4)] and this works So now I try to apply the function to the list: Prelude> map (f) [(a2+b2,a)| a <- [1..4] , b<- [1..4], a2+b2<20, b<=a] and I get this result: <interactive>:1:5: Ambiguous type variable `t' in the constraints: `Integral t' arising from use of `f' at <interactive>:1:5 `RealFrac t' arising from use of `f' at <interactive>:1:5 Probable fix: add a type signature that fixes these type variable(s) I'm sorry, but I don't quite get how to set the type signature and how it will apply to my function... Thanks, Tomi -------------------------------------------------------------------------------- Department for Education, Sport and Culture E Mail This message is for the named person's use only. It may contain confidential, proprietary or legally privileged information. No confidentiality or privilege is waived or lost by any mistransmission. If you receive this message in error, please immediately delete it and all copies of it from your system, destroy any hard copies of it and notify the sender. You must not, directly or indirectly, use, disclose, distribute, print, or copy any part of this message if you are not the intended recipient. The Department for Education, Sport and Culture and any of its establishments each reserve the right to monitor all e-mail communications through its networks. Any views expressed in this message are those of the individual sender, except where the message states otherwise and the sender is authorised to state them to be the views of any such entity. The Department for Education, Sport and Culture shall not be liable to the recipient or any third party for any loss or damage, however it appears, from this e-mail or its content. This includes loss or damage caused by viruses. It is the responsibility of the recipient to ensure that the opening of this message and its attachments shall not adversely affect systems or data. --------------------------------------------------------------------------------
On Sep 5, 2007, at 21:10 , Tomi Owens wrote:
Prelude> let f (a,b) = a * floor (100000/b) Prelude> f(2,5) 40000
This function works just as I want it to.
Now I try creating a list:
Prelude> [(a2+b2,a)| a <- [1..4] , b<- [1..4], a2+b2<20, b<=a] [(2,1),(5,2),(8,2),(10,3),(13,3),(18,3),(17,4)]
and this works So now I try to apply the function to the list:
Prelude> map (f) [(a2+b2,a)| a <- [1..4] , b<- [1..4], a2+b2<20, b<=a]
and I get this result:
<interactive>:1:5: Ambiguous type variable `t' in the constraints: `Integral t' arising from use of `f' at <interactive>:1:5 `RealFrac t' arising from use of `f' at <interactive>:1:5 Probable fix: add a type signature that fixes these type variable (s)
I'm sorry, but I don't quite get how to set the type signature and how it will apply to my function...
The problem here is that (assuming the a\sup{2} etc. are actually a^2) the (^) operator expects and returns Integrals, but (/) requires a RealFrac. Thus, the type of your list comprehension is inferred to be [(Integer,Integer)] but needs to be RealFrac a => [(Integer,a)] (or, more simply, [(Integer,Double)]. Prelude> let f (a,b) = a * floor (100000/b) Prelude> :t f f :: (RealFrac t1, Integral t) => (t, t1) -> t Prelude> let v :: [(Integer,Double)]; v = [(a^2 + b^2,fromIntegral a) | a <- [1..4], b <- [1..4], a^2 + b^2 < 20, b <= a] Prelude> :t v v :: [(Integer, Double)] Prelude> map f v [200000,250000,400000,333330,433329,599994,425000] -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
On 5 Sep 07 23 28, Brandon S. Allbery KF8NH wrote:
On Sep 5, 2007, at 21:10 , Tomi Owens wrote:
Prelude> let f (a,b) = a * floor (100000/b) Prelude> f(2,5) 40000
This function works just as I want it to.
Now I try creating a list:
Prelude> [(a2+b2,a)| a <- [1..4] , b<- [1..4], a2+b2<20, b<=a] [(2,1),(5,2),(8,2),(10,3),(13,3),(18,3),(17,4)]
and this works So now I try to apply the function to the list:
Prelude> map (f) [(a2+b2,a)| a <- [1..4] , b<- [1..4], a2+b2<20, b<=a]
and I get this result:
<interactive>:1:5: Ambiguous type variable `t' in the constraints: `Integral t' arising from use of `f' at <interactive>:1:5 `RealFrac t' arising from use of `f' at <interactive>:1:5 Probable fix: add a type signature that fixes these type variable
(s)
I'm sorry, but I don't quite get how to set the type signature and how it will apply to my function...
The problem here is that (assuming the a\sup{2} etc. are actually a^2) the (^) operator expects and returns Integrals, but (/) requires
a RealFrac. Thus, the type of your list comprehension is inferred to
be [(Integer,Integer)] but needs to be RealFrac a => [(Integer,a)] (or, more simply, [(Integer,Double)].
Prelude> let f (a,b) = a * floor (100000/b) Prelude> :t f f :: (RealFrac t1, Integral t) => (t, t1) -> t Prelude> let v :: [(Integer,Double)]; v = [(a^2 + b^2,fromIntegral
a) | a <- [1..4], b <- [1..4], a^2 + b^2 < 20, b <= a] Prelude> :t v v :: [(Integer, Double)] Prelude> map f v [200000,250000,400000,333330,433329,599994,425000]
-- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
sorry for the empty post before - what i wanted to says is, but the send button was to quickly reached: --------------------- i think this is a perfect example that the statement made earlier, namely that list overloading confuses beginners, but overloading numbers does not, is wrong. i would therefore recommend (1) that all operators in Haskell' are made into classes to allow their overloading - in accordance with the polymorphic character of haskell (today). and (2) that the number system with the default conversions is critically evaluated. in my programming, the overloading of numbers and the defaults come in may way more often than not (i'd rather not have it automatically done! like many other things in haskell which are not automatic). overloading of numbers - as currently done - makes it difficult to use GHCi as a convenient and easy to use desktop calculator - more flexible and powerful than most others and in competition with mathematica and mathcad etc. (there is a large user pool!) andrew On 6 Sep 07 12 22, Andrew U. Frank wrote:
On 5 Sep 07 23 28, Brandon S. Allbery KF8NH wrote:
On Sep 5, 2007, at 21:10 , Tomi Owens wrote:
Prelude> let f (a,b) = a * floor (100000/b) Prelude> f(2,5) 40000
This function works just as I want it to.
Now I try creating a list:
Prelude> [(a2+b2,a)| a <- [1..4] , b<- [1..4], a2+b2<20, b<=a] [(2,1),(5,2),(8,2),(10,3),(13,3),(18,3),(17,4)]
and this works So now I try to apply the function to the list:
Prelude> map (f) [(a2+b2,a)| a <- [1..4] , b<- [1..4], a2+b2<20, b<=a]
and I get this result:
<interactive>:1:5: Ambiguous type variable `t' in the constraints: `Integral t' arising from use of `f' at <interactive>:1:5 `RealFrac t' arising from use of `f' at <interactive>:1:5 Probable fix: add a type signature that fixes these type variable
(s)
I'm sorry, but I don't quite get how to set the type signature
and
how it will apply to my function...
The problem here is that (assuming the a\sup{2} etc. are actually a^2) the (^) operator expects and returns Integrals, but (/) requires
a RealFrac. Thus, the type of your list comprehension is inferred to
be [(Integer,Integer)] but needs to be RealFrac a => [(Integer,a)] (or, more simply, [(Integer,Double)].
Prelude> let f (a,b) = a * floor (100000/b) Prelude> :t f f :: (RealFrac t1, Integral t) => (t, t1) -> t Prelude> let v :: [(Integer,Double)]; v = [(a^2 + b^2,fromIntegral
a) | a <- [1..4], b <- [1..4], a^2 + b^2 < 20, b <= a] Prelude> :t v v :: [(Integer, Double)] Prelude> map f v [200000,250000,400000,333330,433329,599994,425000]
-- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
"Tomi Owens" <t.owens@hautlieu.sch.je> writes:
Hi there. I'm a teacher of Maths and am working my way through the Euler Project problems for fun. I have mostly been using Basic, but have read up about Haskell and think it looks like a sensible way to solve many of the problems.
It certainly is.
Prelude> let f (a,b) = a * floor (100000/b)
From the floor and the rest of your message, it looks like you want truncating integer division here. So, instead of floor and (/), use div.
let f (a,b) = a * 100000 `div` b With that change the rest of your program should work. The other replies explain why the interpreter couldn't figure out which type to use.
participants (4)
-
Andrew U. Frank -
Brandon S. Allbery KF8NH -
Chris Mears -
Tomi Owens