[Haskell-cafe] Re: [Haskell] (no subject)
Thomas Hartman
thomas.hartman at db.com
Wed Sep 5 18:29:58 EDT 2007
I think you want something like this
{-# OPTIONS -fglasgow-exts #-}
f :: (Integer, Float) -> Integer
f (a,b) = a * floor (100000/b)
lst :: [(Integer, Integer)]
lst = [(a ^ 2 + b ^ 2, a) | a <- [1..4], b <- [1..4], a^2 + b^2 < 20, b <=
a]
lst3 = map (f) ( map ( intTupToFloatTup ) lst )
intTupToFloatTup :: (Integer, Integer) -> (Integer, Float)
intTupToFloatTup (int1, int2) = (int1, fromInteger int2)
load the whole thing into ghci with ghci proggie.hs
when I have this type of problem, my usual approach is to put the code
into a text file, load that in ghci, derive type sigs on the functions
that work, and then see if I can figure out
the mismatch.
you could probably get a fast answer to this kind of question on the
#haskell irc channel as well.
hope this helps,
thomas.
"Scott Williams" <williasr at google.com>
Sent by: haskell-cafe-bounces at haskell.org
09/05/2007 05:28 PM
To
"Tomi Owens" <t.owens at hautlieu.sch.je>
cc
haskell-cafe at haskell.org
Subject
[Haskell-cafe] Re: [Haskell] (no subject)
[bcc haskell, cc haskell-cafe]
On 9/5/07, Tomi Owens <t.owens at hautlieu.sch.je> wrote:
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
Here you can find out type ghci has inferred for this function.
> :t f
f :: (RealFrac b, Integral b1) => (b1, b) -> b1
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)]
Let's assign this to an intermediate variable so we can query it's type:
Prelude> let lst = [(a ^ 2 + b ^ 2, a) | a <- [1..4], b <- [1..4], a^2 +
b^2 < 20, b <= a]
Prelude> lst
[(2,1),(5,2),(8,2),(10,3),(13,3),(18,3),(17,4)]
Prelude> :t lst
lst :: [(Integer, Integer)]
aha; here's the source of the type mismatch:
Prelude> :t floor
floor :: (RealFrac a, Integral b) => a -> b
Floor has to take a RealFrac. According to hoogle[1], we can use various
floating-point approximations (Float, Double, CFloat, etc) or we can use
the exact Rational type.
[1] http://haskell.org/hoogle/?q=RealFrac
You can get your types to match by declaring your list to be of type
[(Rational, Rational)] either by explicitly typing one of the untyped
variables or the entire expression:
Prelude> let lst = [(a ^ 2 + b ^ 2, a) | (a::Rational) <- [1..4], b <-
[1..4], a^2 + b^2 < 20, b <= a]
Prelude> :t lst
lst :: [(Rational, Rational)]
Prelude> let lst :: [(Rational, Rational)] = [(a ^ 2 + b ^ 2, a) | a <-
[1..4], b <- [1..4], a^2 + b^2 < 20, b <= a]
Prelude> :t lst
lst :: [(Rational, Rational)]
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,
Hope this helps
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.
--------------------------------------------------------------------------------
_______________________________________________
Haskell mailing list
Haskell at haskell.org
http://www.haskell.org/mailman/listinfo/haskell
--
Scott Williams _______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe at haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
---
This e-mail may contain confidential and/or privileged information. If you
are not the intended recipient (or have received this e-mail in error)
please notify the sender immediately and destroy this e-mail. Any
unauthorized copying, disclosure or distribution of the material in this
e-mail is strictly forbidden.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070905/92fbc856/attachment-0001.htm
More information about the Haskell-Cafe
mailing list