Closed oneself closed 12 years ago
Hi,
I didn't write this book. I've just created the necessary files required for mobi generation.
Anyways, there's no error in that example. The length'
function receives a list which may contain values of any type (hence the a
, which basically means I don't care what are the types of the elements in this list), and returns a number. In Haskell there are several types that represent numbers, but all of them are instances of what is called a type-class (a type-class is similar to an interface in Java). That type-class is Num
and in this function the type variable b
is declared to be an instance of it. That's why it returns b
. It can be any number type as long as it is an instance of the Num
type-class. The function calculates the length, so it's got to be a number.
You can list the methods of the Num
type-class in a GHCi prompt like this:
Prelude> :i Num
class Num a where
(+) :: a -> a -> a
(*) :: a -> a -> a
(-) :: a -> a -> a
negate :: a -> a
abs :: a -> a
signum :: a -> a
fromInteger :: Integer -> a
-- Defined in `GHC.Num'
instance Num Integer -- Defined in `GHC.Num'
instance Num Int -- Defined in `GHC.Num'
instance Num Float -- Defined in `GHC.Float'
instance Num Double -- Defined in `GHC.Float'
Also, write and load in the interpreter the function that you think is wrong and the function that you think is right, and see what it happens.
Hope it helps.
P.S. If you have further questions, please use the Haskell Cafe mailing list, the #haskell IRC channel or StackOverflow.
You right, I had a different error elsewhere.
Sorry and thank you.
On Thu, Jul 19, 2012 at 12:17 PM, Ionuț G. Stan < reply@reply.github.com
wrote:
Hi,
I didn't write this book. I've just created the necessary files required for mobi generation.
Anyways, there's no error in that example. The
length'
function receives a list which may contain values of any type (hence thea
, which basically means I don't care what are the types of the elements in this list), and returns a number. In Haskell there are several types that represent numbers, but all of them are instances of what is called a type-class (a type-class is similar to an interface in Java). That type-class isNum
and in this function the type variableb
is declared to be an instance of it. That's why it returnsb
. It can be any number type as long as it is an instance of theNum
type-class. The function calculates the length, so it's got to be a number.You can list the methods of the
Num
type-class in a GHCi prompt like this:Prelude> :i Num class Num a where (+) :: a -> a -> a (*) :: a -> a -> a (-) :: a -> a -> a negate :: a -> a abs :: a -> a signum :: a -> a fromInteger :: Integer -> a -- Defined in `GHC.Num' instance Num Integer -- Defined in `GHC.Num' instance Num Int -- Defined in `GHC.Num' instance Num Float -- Defined in `GHC.Float' instance Num Double -- Defined in `GHC.Float'
Also, write and load in the interpreter the function that you think is wrong and the function that you think is right, and see what it happens.
Hope it helps.
P.S. If you have further questions, please use the Haskell Cafe mailing list, the #haskell IRC channel or StackOverflow.
Reply to this email directly or view it on GitHub:
https://github.com/igstan/learn-you-a-haskell-kindle/pull/4#issuecomment-7102900
_Eyal Erez <_oneself@gmail.com oneself@gmail.com>
There are 10 types of people, those who know binary and those who don't.
Hi, I love the book. Thank you very much for writing it.
I believe there's a compile error in the recursive "length'" example.