I have a list. Each component is a list with 2 whole numbers. I want
to multiply the second number by the log of the first eg tail ([519432,525806]) * log (head [519432,525806]). I get the ffg error message: No instance for (Floating [t]) arising from a use of `log' at <interactive>:1:25-50 Possible fix: add an instance declaration for (Floating [t]) In the second argument of `(*)', namely `log (head [519432, 525806])' In the expression: tail ([519432, 525806]) * log (head [519432, 525806]) In the definition of `it': it = tail ([519432, 525806]) * log (head [519432, 525806]) I've tried the only type signature I know "fromIntegral" but it does not help. How to fix? Thanks Logesh _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
On 1 Feb 2008, at 10:30 PM, Logesh Pillay wrote:
> I have a list. Each component is a list with 2 whole numbers. First off, why? In Haskell (unlike dynamically typed languages like Python or Perl), if you know you always have two elements, you want a pair (519432,525806), not a list [519432,525806]. > I want to multiply the second number by the log of the first > eg > tail ([519432,525806]) * log (head [519432,525806]). You mean [519432,525806] !! 1 * log ([519432,525806 !! 0) or fst (519432,525806) * log (snd (519432,525806) or (best of all) case (519432, 525806) of (x, y) -> x * log y or (if you must have a list) case [519432, 525806] of [x, y] -> x * log y > I get the ffg error message: > > No instance for (Floating [t]) This is because tail :: [t] -> [t] It doesn't give you a single element; it gives you the whole list! jcc _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
In reply to this post by Logesh Pillay-3
Look at the type of tail:
tail :: [a] -> [a] That is, tail is the list of all elements *except* the head. You want "last". (Barring style considerations. Usually in a situation like this you would use a list of tuples rather than a list of lists, since then you know at compile time that you have exactly two numbers in each element of the list). Luke On Feb 2, 2008 6:30 AM, Logesh Pillay <[hidden email]> wrote: > I have a list. Each component is a list with 2 whole numbers. I want > to multiply the second number by the log of the first > eg > tail ([519432,525806]) * log (head [519432,525806]). > > I get the ffg error message: > > No instance for (Floating [t]) > arising from a use of `log' at <interactive>:1:25-50 > Possible fix: add an instance declaration for (Floating [t]) > In the second argument of `(*)', namely > `log (head [519432, 525806])' > In the expression: > tail ([519432, 525806]) * log (head [519432, 525806]) > In the definition of `it': > it = tail ([519432, 525806]) * log (head [519432, 525806]) > > > I've tried the only type signature I know "fromIntegral" but it does not > help. > > How to fix? > > Thanks > Logesh > > > _______________________________________________ > Haskell-Cafe mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/haskell-cafe > Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
Free forum by Nabble | Edit this page |