[Haskell-cafe] Newbie question
Hi, I try to undestand why this code dosen't work f :: (Num a)=Integer-a f i = i Integer is an instance of Num, so why does this code produce error: Couldn't match expected type 'a' againsta inferred type 'Integer' ... ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Newbie question
How does caller choose which particular instance of Num they want? In object-oriented language If function return type is an interface it means that it can return any implementation of this interface, but caller can't choose which particular inplementation they want. What the difference between haskell class and interface in object-oriented languge such Java or C#? 2008/1/21, Brent Yorgey [EMAIL PROTECTED]: 2008/1/21 Alexander Seliverstov [EMAIL PROTECTED]: Hi, I try to undestand why this code dosen't work f :: (Num a)=Integer-a f i = i Integer is an instance of Num, so why does this code produce error: Couldn't match expected type 'a' againsta inferred type 'Integer' ... But the type of this function says that it can return *any* instance of Num -- that is, the caller gets to choose which particular instance of Num they want. This function can only ever return an Integer. There is actually a function of this type, however; it's called fromIntegral. It works because it is a member of the Num type class. -Brent ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Newbie question
So, the function type (Num a)=Integer-a means that return value of this function can be cast to any particular instance of class Num. Ok. I have a my own class class A a and want to write function like this f:: (A a)=Integer-a. Can I do it? 2008/1/21, Jon Fairbairn [EMAIL PROTECTED]: Alexander Seliverstov [EMAIL PROTECTED] writes: How does caller choose which particular instance of Num they want? They specify the type... or just pass the result to something that specifies the type. Try it in ghci: Prelude let f:: Integral i = Integer - i; f = fromIntegral Prelude let g :: Int - Int; g = id Prelude :t g (f 5) g (f 5) :: Int Prelude let h :: Integer - Integer; h = id Prelude :t h (f 5) h (f 5) :: Integer Prelude What the difference between haskell class and interface in object-oriented languge such Java or C#? Really they are completely different animals that look a lot alike because they serve similar purposes -- convergent evolution! -- Jón Fairbairn [EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe -- С уважением, Селиверстов Александр ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe