Learn you a Haskell for Great Good

moo

Believe the type

Previously we mentioned that Haskell has a static type system. The type of every expression is known at compile time, which leads to safer code. If you write a program where you try to divide a boolean type with some number, it won't even compile. That's good because it's better to catch such errors at compile time instead of having your program crash. Everything in Haskell has a type, so the compiler can reason quite a lot about your program before compiling it.

Unlike Java or Pascal, Haskell has type inference. If we write a number, we don't have to tell Haskell it's a number. It can infer that on its own, so we don't have to explicitly write out the types of our functions and expressions to get things done. We covered some of the basics of Haskell with only a very superficial glance at types. However, understanding the type system is a very important part of learning Haskell.

A type is a kind of label that every expression has. It tells us in which category of things that expression fits. The expression True is a boolean, "hello" is a string, etc.

bombNow we'll use GHCI to examine the types of some expressions. We'll do that by using the :t command which, followed by any valid expression, tells us its type. Let's give it a whirl.

ghci> :t 'a' 'a' :: Char ghci> :t True True :: Bool ghci> :t "HELLO!" "HELLO!" :: [Char] ghci> :t (True, 'a') (True, 'a') :: (Bool, Char) ghci> :t 4 == 5 4 == 5 :: Bool

Here we see that doing :t on an expression prints out the expression followed by :: and its type. :: is read as "has type of". Explicit types are always denoted with the first letter in capital case. 'a', as it would seem, has a type of Char. It's not hard to conclude that it stands for character. True is of a Bool type. That makes sense. But what's this? Examining the type of "HELLO!" yields a [Char]. The square brackets denote a list. So we read that as it being a list of characters. Unlike lists, each tuple length has its own type. So the expression of (True, 'a') has a type of (Bool, Char), whereas an expression such as ('a', 'b', 'c') would have the type of (Char, Char, Char). 4 == 5 will always return False, so its type is Bool.

Functions also have types. When writing our own functions, we can choose to give them an explicit type declaration. This is generally considered to be good practice except when writing very short functions. From here on, we'll give all the functions that we make type declarations. Remember the list comprehension we made previously that filters a string so that only caps remain? Here's how it looks like with a type declaration.

You might also like

Learn A Great New Dance For (And With) Your Kids!
Learn A Great New Dance For (And With) Your Kids!
Learn what it takes to make a good Android app great
Learn what it takes to make a good Android app great
Learn You A Keyboard Macros For Great Good: A VimGolf in
Learn You A Keyboard Macros For Great Good: A VimGolf in ...
Learn you some Ansible for great good!
Learn you some Ansible for great good!
Learn You Some Lisp for Great Good
Learn You Some Lisp for Great Good
Andrew Buntine: Learn you a parallax for great good
Andrew Buntine: Learn you a parallax for great good

Q&A

avatar
What is a good Haskell tutorial that offers learning by small task programming? - Quora

Real World Haskell has a number of workable examples and exercises many of which are not math related, such as doing simple types of parsing, or image processing to detect barcodes.


Copyright © . All Rights Reserved