2013-06-29 00:01:58 +00:00
|
|
|
|
---
|
2014-10-12 19:24:52 +00:00
|
|
|
|
language: Haskell
|
2017-08-25 08:01:38 +00:00
|
|
|
|
filename: learnhaskell.hs
|
2013-07-04 05:59:13 +00:00
|
|
|
|
contributors:
|
|
|
|
|
- ["Adit Bhargava", "http://adit.io"]
|
2013-06-29 00:01:58 +00:00
|
|
|
|
---
|
|
|
|
|
|
2013-12-02 12:09:58 +00:00
|
|
|
|
Haskell was designed as a practical, purely functional programming
|
|
|
|
|
language. It's famous for its monads and its type system, but I keep coming back
|
|
|
|
|
to it because of its elegance. Haskell makes coding a real joy for me.
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
|
|
|
|
```haskell
|
|
|
|
|
-- Single line comments start with two dashes.
|
|
|
|
|
{- Multiline comments can be enclosed
|
2013-10-23 19:19:55 +00:00
|
|
|
|
in a block like this.
|
2013-06-29 00:01:58 +00:00
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
----------------------------------------------------
|
|
|
|
|
-- 1. Primitive Datatypes and Operators
|
|
|
|
|
----------------------------------------------------
|
|
|
|
|
|
|
|
|
|
-- You have numbers
|
|
|
|
|
3 -- 3
|
|
|
|
|
|
|
|
|
|
-- Math is what you would expect
|
|
|
|
|
1 + 1 -- 2
|
|
|
|
|
8 - 1 -- 7
|
|
|
|
|
10 * 2 -- 20
|
|
|
|
|
35 / 5 -- 7.0
|
|
|
|
|
|
|
|
|
|
-- Division is not integer division by default
|
|
|
|
|
35 / 4 -- 8.75
|
|
|
|
|
|
|
|
|
|
-- integer division
|
|
|
|
|
35 `div` 4 -- 8
|
|
|
|
|
|
|
|
|
|
-- Boolean values are primitives
|
|
|
|
|
True
|
|
|
|
|
False
|
|
|
|
|
|
|
|
|
|
-- Boolean operations
|
|
|
|
|
not True -- False
|
|
|
|
|
not False -- True
|
2021-10-31 11:58:33 +00:00
|
|
|
|
True && False -- False
|
|
|
|
|
True || False -- True
|
2013-06-29 00:01:58 +00:00
|
|
|
|
1 == 1 -- True
|
|
|
|
|
1 /= 1 -- False
|
|
|
|
|
1 < 10 -- True
|
|
|
|
|
|
2013-06-30 17:03:42 +00:00
|
|
|
|
-- In the above examples, `not` is a function that takes one value.
|
|
|
|
|
-- Haskell doesn't need parentheses for function calls...all the arguments
|
|
|
|
|
-- are just listed after the function. So the general pattern is:
|
|
|
|
|
-- func arg1 arg2 arg3...
|
|
|
|
|
-- See the section on functions for information on how to write your own.
|
|
|
|
|
|
2013-06-29 00:01:58 +00:00
|
|
|
|
-- Strings and characters
|
|
|
|
|
"This is a string."
|
|
|
|
|
'a' -- character
|
|
|
|
|
'You cant use single quotes for strings.' -- error!
|
|
|
|
|
|
2013-06-30 17:03:42 +00:00
|
|
|
|
-- Strings can be concatenated
|
2013-06-29 00:09:34 +00:00
|
|
|
|
"Hello " ++ "world!" -- "Hello world!"
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
2013-06-30 17:03:42 +00:00
|
|
|
|
-- A string is a list of characters
|
2015-02-01 21:39:52 +00:00
|
|
|
|
['H', 'e', 'l', 'l', 'o'] -- "Hello"
|
2020-10-15 19:29:37 +00:00
|
|
|
|
|
|
|
|
|
-- Lists can be indexed with the `!!` operator followed by an index
|
2013-06-29 00:09:34 +00:00
|
|
|
|
"This is a string" !! 0 -- 'T'
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
----------------------------------------------------
|
2017-04-01 20:19:58 +00:00
|
|
|
|
-- 2. Lists and Tuples
|
2013-06-29 00:01:58 +00:00
|
|
|
|
----------------------------------------------------
|
|
|
|
|
|
|
|
|
|
-- Every element in a list must have the same type.
|
2017-02-09 15:26:11 +00:00
|
|
|
|
-- These two lists are equal:
|
2013-06-29 00:01:58 +00:00
|
|
|
|
[1, 2, 3, 4, 5]
|
|
|
|
|
[1..5]
|
|
|
|
|
|
2015-02-01 00:40:55 +00:00
|
|
|
|
-- Ranges are versatile.
|
|
|
|
|
['A'..'F'] -- "ABCDEF"
|
|
|
|
|
|
|
|
|
|
-- You can create a step in a range.
|
|
|
|
|
[0,2..10] -- [0, 2, 4, 6, 8, 10]
|
2017-02-09 15:26:11 +00:00
|
|
|
|
[5..1] -- [] (Haskell defaults to incrementing)
|
2015-02-01 00:40:55 +00:00
|
|
|
|
[5,4..1] -- [5, 4, 3, 2, 1]
|
|
|
|
|
|
2015-03-01 19:16:29 +00:00
|
|
|
|
-- indexing into a list
|
2017-02-09 15:26:11 +00:00
|
|
|
|
[1..10] !! 3 -- 4 (zero-based indexing)
|
2015-03-01 19:16:29 +00:00
|
|
|
|
|
2013-06-29 00:01:58 +00:00
|
|
|
|
-- You can also have infinite lists in Haskell!
|
|
|
|
|
[1..] -- a list of all the natural numbers
|
|
|
|
|
|
2013-06-30 17:03:42 +00:00
|
|
|
|
-- Infinite lists work because Haskell has "lazy evaluation". This means
|
|
|
|
|
-- that Haskell only evaluates things when it needs to. So you can ask for
|
|
|
|
|
-- the 1000th element of your list and Haskell will give it to you:
|
|
|
|
|
|
|
|
|
|
[1..] !! 999 -- 1000
|
|
|
|
|
|
|
|
|
|
-- And now Haskell has evaluated elements 1 - 1000 of this list...but the
|
|
|
|
|
-- rest of the elements of this "infinite" list don't exist yet! Haskell won't
|
|
|
|
|
-- actually evaluate them until it needs to.
|
|
|
|
|
|
2013-08-12 15:17:37 +00:00
|
|
|
|
-- joining two lists
|
2013-06-29 00:01:58 +00:00
|
|
|
|
[1..5] ++ [6..10]
|
|
|
|
|
|
|
|
|
|
-- adding to the head of a list
|
|
|
|
|
0:[1..5] -- [0, 1, 2, 3, 4, 5]
|
|
|
|
|
|
|
|
|
|
-- more list operations
|
|
|
|
|
head [1..5] -- 1
|
|
|
|
|
tail [1..5] -- [2, 3, 4, 5]
|
|
|
|
|
init [1..5] -- [1, 2, 3, 4]
|
|
|
|
|
last [1..5] -- 5
|
|
|
|
|
|
|
|
|
|
-- list comprehensions
|
|
|
|
|
[x*2 | x <- [1..5]] -- [2, 4, 6, 8, 10]
|
|
|
|
|
|
|
|
|
|
-- with a conditional
|
2013-06-29 00:09:34 +00:00
|
|
|
|
[x*2 | x <- [1..5], x*2 > 4] -- [6, 8, 10]
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
2013-06-29 03:53:43 +00:00
|
|
|
|
-- Every element in a tuple can be a different type, but a tuple has a
|
|
|
|
|
-- fixed length.
|
2013-06-29 00:01:58 +00:00
|
|
|
|
-- A tuple:
|
|
|
|
|
("haskell", 1)
|
|
|
|
|
|
2014-11-21 17:28:38 +00:00
|
|
|
|
-- accessing elements of a pair (i.e. a tuple of length 2)
|
2013-06-29 00:01:58 +00:00
|
|
|
|
fst ("haskell", 1) -- "haskell"
|
|
|
|
|
snd ("haskell", 1) -- 1
|
|
|
|
|
|
2018-07-10 22:12:23 +00:00
|
|
|
|
-- pair element accessing does not work on n-tuples (i.e. triple, quadruple, etc)
|
2018-07-10 22:34:42 +00:00
|
|
|
|
snd ("snd", "can't touch this", "da na na na") -- error! see function below
|
2018-07-10 22:12:23 +00:00
|
|
|
|
|
2013-06-29 00:01:58 +00:00
|
|
|
|
----------------------------------------------------
|
|
|
|
|
-- 3. Functions
|
|
|
|
|
----------------------------------------------------
|
|
|
|
|
-- A simple function that takes two variables
|
|
|
|
|
add a b = a + b
|
|
|
|
|
|
2013-06-29 21:17:52 +00:00
|
|
|
|
-- Note that if you are using ghci (the Haskell interpreter)
|
|
|
|
|
-- You'll need to use `let`, i.e.
|
|
|
|
|
-- let add a b = a + b
|
|
|
|
|
|
2013-06-29 00:01:58 +00:00
|
|
|
|
-- Using the function
|
|
|
|
|
add 1 2 -- 3
|
|
|
|
|
|
2013-06-29 03:53:43 +00:00
|
|
|
|
-- You can also put the function name between the two arguments
|
|
|
|
|
-- with backticks:
|
2013-06-29 00:01:58 +00:00
|
|
|
|
1 `add` 2 -- 3
|
|
|
|
|
|
2013-08-08 19:05:01 +00:00
|
|
|
|
-- You can also define functions that have no letters! This lets
|
2013-06-29 03:53:43 +00:00
|
|
|
|
-- you define your own operators! Here's an operator that does
|
|
|
|
|
-- integer division
|
2013-06-29 00:01:58 +00:00
|
|
|
|
(//) a b = a `div` b
|
|
|
|
|
35 // 4 -- 8
|
|
|
|
|
|
|
|
|
|
-- Guards: an easy way to do branching in functions
|
|
|
|
|
fib x
|
2015-03-16 20:07:19 +00:00
|
|
|
|
| x < 2 = 1
|
2013-06-29 00:01:58 +00:00
|
|
|
|
| otherwise = fib (x - 1) + fib (x - 2)
|
|
|
|
|
|
2013-06-29 00:09:34 +00:00
|
|
|
|
-- Pattern matching is similar. Here we have given three different
|
2017-02-09 15:27:29 +00:00
|
|
|
|
-- equations that define fib. Haskell will automatically use the first
|
2017-02-09 15:26:11 +00:00
|
|
|
|
-- equation whose left hand side pattern matches the value.
|
2013-06-29 00:01:58 +00:00
|
|
|
|
fib 1 = 1
|
|
|
|
|
fib 2 = 2
|
|
|
|
|
fib x = fib (x - 1) + fib (x - 2)
|
|
|
|
|
|
2018-07-10 22:15:26 +00:00
|
|
|
|
-- Pattern matching on tuples
|
2018-07-10 22:34:42 +00:00
|
|
|
|
sndOfTriple (_, y, _) = y -- use a wild card (_) to bypass naming unused value
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
2013-07-01 15:33:25 +00:00
|
|
|
|
-- Pattern matching on lists. Here `x` is the first element
|
|
|
|
|
-- in the list, and `xs` is the rest of the list. We can write
|
2013-06-29 00:09:34 +00:00
|
|
|
|
-- our own map function:
|
2013-07-01 15:33:25 +00:00
|
|
|
|
myMap func [] = []
|
2013-06-30 17:03:42 +00:00
|
|
|
|
myMap func (x:xs) = func x:(myMap func xs)
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
2013-06-29 03:53:43 +00:00
|
|
|
|
-- Anonymous functions are created with a backslash followed by
|
|
|
|
|
-- all the arguments.
|
2013-06-30 17:03:42 +00:00
|
|
|
|
myMap (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7]
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
2013-06-29 03:53:43 +00:00
|
|
|
|
-- using fold (called `inject` in some languages) with an anonymous
|
|
|
|
|
-- function. foldl1 means fold left, and use the first value in the
|
2013-07-01 15:33:25 +00:00
|
|
|
|
-- list as the initial value for the accumulator.
|
2013-06-29 00:01:58 +00:00
|
|
|
|
foldl1 (\acc x -> acc + x) [1..5] -- 15
|
|
|
|
|
|
|
|
|
|
----------------------------------------------------
|
2013-06-29 00:09:34 +00:00
|
|
|
|
-- 4. More functions
|
2013-06-29 00:01:58 +00:00
|
|
|
|
----------------------------------------------------
|
|
|
|
|
|
2014-10-21 22:30:30 +00:00
|
|
|
|
-- partial application: if you don't pass in all the arguments to a function,
|
2015-03-16 20:07:19 +00:00
|
|
|
|
-- it gets "partially applied". That means it returns a function that takes the
|
2013-06-29 00:09:34 +00:00
|
|
|
|
-- rest of the arguments.
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
|
|
|
|
add a b = a + b
|
|
|
|
|
foo = add 10 -- foo is now a function that takes a number and adds 10 to it
|
|
|
|
|
foo 5 -- 15
|
|
|
|
|
|
|
|
|
|
-- Another way to write the same thing
|
2015-10-23 21:31:10 +00:00
|
|
|
|
foo = (10+)
|
2013-06-29 00:01:58 +00:00
|
|
|
|
foo 5 -- 15
|
|
|
|
|
|
|
|
|
|
-- function composition
|
2015-10-31 12:37:13 +00:00
|
|
|
|
-- the operator `.` chains functions together.
|
2013-06-29 00:09:34 +00:00
|
|
|
|
-- For example, here foo is a function that takes a value. It adds 10 to it,
|
2015-10-20 19:05:16 +00:00
|
|
|
|
-- multiplies the result of that by 4, and then returns the final value.
|
2015-10-23 21:31:10 +00:00
|
|
|
|
foo = (4*) . (10+)
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
2017-02-09 15:27:29 +00:00
|
|
|
|
-- 4*(10+5) = 60
|
2015-10-20 19:05:16 +00:00
|
|
|
|
foo 5 -- 60
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
|
|
|
|
-- fixing precedence
|
2018-07-10 22:12:23 +00:00
|
|
|
|
-- Haskell has an operator called `$`. This operator applies a function
|
|
|
|
|
-- to a given parameter. In contrast to standard function application, which
|
|
|
|
|
-- has highest possible priority of 10 and is left-associative, the `$` operator
|
2015-03-27 13:25:33 +00:00
|
|
|
|
-- has priority of 0 and is right-associative. Such a low priority means that
|
2018-08-02 04:12:35 +00:00
|
|
|
|
-- the expression on its right is applied as a parameter to the function on its left.
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
|
|
|
|
-- before
|
2015-07-06 08:40:05 +00:00
|
|
|
|
even (fib 7) -- false
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
2014-11-21 17:28:38 +00:00
|
|
|
|
-- equivalently
|
2015-03-16 19:24:21 +00:00
|
|
|
|
even $ fib 7 -- false
|
2014-11-21 17:28:38 +00:00
|
|
|
|
|
2015-07-06 08:40:05 +00:00
|
|
|
|
-- composing functions
|
|
|
|
|
even . fib $ 7 -- false
|
|
|
|
|
|
|
|
|
|
|
2013-06-29 00:01:58 +00:00
|
|
|
|
----------------------------------------------------
|
|
|
|
|
-- 5. Type signatures
|
|
|
|
|
----------------------------------------------------
|
|
|
|
|
|
2018-07-10 22:12:23 +00:00
|
|
|
|
-- Haskell has a very strong type system, and every valid expression has a type.
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
2013-06-29 00:09:34 +00:00
|
|
|
|
-- Some basic types:
|
2013-06-29 00:01:58 +00:00
|
|
|
|
5 :: Integer
|
|
|
|
|
"hello" :: String
|
|
|
|
|
True :: Bool
|
|
|
|
|
|
2013-06-29 00:09:34 +00:00
|
|
|
|
-- Functions have types too.
|
|
|
|
|
-- `not` takes a boolean and returns a boolean:
|
2013-06-30 17:03:42 +00:00
|
|
|
|
-- not :: Bool -> Bool
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
2013-06-29 00:09:34 +00:00
|
|
|
|
-- Here's a function that takes two arguments:
|
2013-06-30 17:03:42 +00:00
|
|
|
|
-- add :: Integer -> Integer -> Integer
|
|
|
|
|
|
2013-07-01 15:50:25 +00:00
|
|
|
|
-- When you define a value, it's good practice to write its type above it:
|
2013-06-30 17:03:42 +00:00
|
|
|
|
double :: Integer -> Integer
|
|
|
|
|
double x = x * 2
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
|
|
|
|
----------------------------------------------------
|
2014-11-21 17:28:38 +00:00
|
|
|
|
-- 6. Control Flow and If Expressions
|
2013-06-29 00:01:58 +00:00
|
|
|
|
----------------------------------------------------
|
|
|
|
|
|
2017-02-09 15:26:11 +00:00
|
|
|
|
-- if-expressions
|
2013-06-29 00:01:58 +00:00
|
|
|
|
haskell = if 1 == 1 then "awesome" else "awful" -- haskell = "awesome"
|
|
|
|
|
|
2017-02-09 15:26:11 +00:00
|
|
|
|
-- if-expressions can be on multiple lines too, indentation is important
|
2013-06-29 00:01:58 +00:00
|
|
|
|
haskell = if 1 == 1
|
|
|
|
|
then "awesome"
|
|
|
|
|
else "awful"
|
|
|
|
|
|
2014-11-21 17:28:38 +00:00
|
|
|
|
-- case expressions: Here's how you could parse command line arguments
|
2013-06-29 00:01:58 +00:00
|
|
|
|
case args of
|
|
|
|
|
"help" -> printHelp
|
|
|
|
|
"start" -> startProgram
|
|
|
|
|
_ -> putStrLn "bad args"
|
|
|
|
|
|
2014-11-21 17:28:38 +00:00
|
|
|
|
-- Haskell doesn't have loops; it uses recursion instead.
|
2015-10-23 21:31:10 +00:00
|
|
|
|
-- map applies a function over every element in a list
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
|
|
|
|
map (*2) [1..5] -- [2, 4, 6, 8, 10]
|
|
|
|
|
|
|
|
|
|
-- you can make a for function using map
|
|
|
|
|
for array func = map func array
|
|
|
|
|
|
|
|
|
|
-- and then use it
|
2013-06-29 00:09:34 +00:00
|
|
|
|
for [0..5] $ \i -> show i
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
2013-06-29 00:09:34 +00:00
|
|
|
|
-- we could've written that like this too:
|
|
|
|
|
for [0..5] show
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
2013-07-02 18:00:30 +00:00
|
|
|
|
-- You can use foldl or foldr to reduce a list
|
|
|
|
|
-- foldl <fn> <initial value> <list>
|
|
|
|
|
foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43
|
|
|
|
|
|
|
|
|
|
-- This is the same as
|
|
|
|
|
(2 * (2 * (2 * 4 + 1) + 2) + 3)
|
|
|
|
|
|
2015-10-23 21:31:10 +00:00
|
|
|
|
-- foldl is left-handed, foldr is right-handed
|
2013-07-02 18:00:30 +00:00
|
|
|
|
foldr (\x y -> 2*x + y) 4 [1,2,3] -- 16
|
|
|
|
|
|
|
|
|
|
-- This is now the same as
|
2015-04-27 08:16:16 +00:00
|
|
|
|
(2 * 1 + (2 * 2 + (2 * 3 + 4)))
|
2013-07-02 18:00:30 +00:00
|
|
|
|
|
2013-06-29 00:01:58 +00:00
|
|
|
|
----------------------------------------------------
|
|
|
|
|
-- 7. Data Types
|
|
|
|
|
----------------------------------------------------
|
|
|
|
|
|
2019-11-04 16:51:48 +00:00
|
|
|
|
-- A data type is declared with a 'type constructor' on the left
|
|
|
|
|
-- and one or more 'data constructors' on the right, separated by
|
|
|
|
|
-- the pipe | symbol. This is a sum/union type. Each data constructor
|
|
|
|
|
-- is a (possibly nullary) function that creates an object of the type
|
|
|
|
|
-- named by the type constructor.
|
|
|
|
|
|
|
|
|
|
-- This is essentially an enum
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
|
|
|
|
data Color = Red | Blue | Green
|
|
|
|
|
|
2013-06-29 00:09:34 +00:00
|
|
|
|
-- Now you can use it in a function:
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
2013-07-04 05:52:48 +00:00
|
|
|
|
say :: Color -> String
|
2017-02-09 15:26:11 +00:00
|
|
|
|
say Red = "You are Red!"
|
|
|
|
|
say Blue = "You are Blue!"
|
|
|
|
|
say Green = "You are Green!"
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
2019-11-04 16:51:48 +00:00
|
|
|
|
-- Note that the type constructor is used in the type signature
|
|
|
|
|
-- and the data constructors are used in the body of the function
|
|
|
|
|
-- Data constructors are primarily pattern-matched against
|
|
|
|
|
|
|
|
|
|
-- This next one is a traditional container type holding two fields
|
|
|
|
|
-- In a type declaration, data constructors take types as parameters
|
|
|
|
|
-- Data constructors can have the same name as type constructors
|
|
|
|
|
-- This is common where the type only has a single data constructor
|
|
|
|
|
|
|
|
|
|
data Point = Point Float Float
|
|
|
|
|
|
|
|
|
|
-- This can be used in a function like:
|
|
|
|
|
|
|
|
|
|
distance :: Point -> Point -> Float
|
|
|
|
|
distance (Point x y) (Point x' y') = sqrt $ dx + dy
|
|
|
|
|
where dx = (x - x') ** 2
|
|
|
|
|
dy = (y - y') ** 2
|
|
|
|
|
|
|
|
|
|
-- Types can have multiple data constructors with arguments, too
|
|
|
|
|
|
2019-11-04 17:05:21 +00:00
|
|
|
|
data Name = Mononym String
|
|
|
|
|
| FirstLastName String String
|
|
|
|
|
| FullName String String String
|
2019-11-04 16:51:48 +00:00
|
|
|
|
|
|
|
|
|
-- To make things clearer we can use record syntax
|
|
|
|
|
|
2019-11-04 17:05:21 +00:00
|
|
|
|
data Point2D = CartesianPoint2D { x :: Float, y :: Float }
|
|
|
|
|
| PolarPoint2D { r :: Float, theta :: Float }
|
2019-11-04 16:51:48 +00:00
|
|
|
|
|
|
|
|
|
myPoint = CartesianPoint2D { x = 7.0, y = 10.0 }
|
|
|
|
|
|
2019-11-04 17:05:21 +00:00
|
|
|
|
-- Using record syntax automatically creates accessor functions
|
|
|
|
|
-- (the name of the field)
|
2019-11-04 16:51:48 +00:00
|
|
|
|
|
|
|
|
|
xOfMyPoint = x myPoint
|
|
|
|
|
|
|
|
|
|
-- xOfMyPoint is equal to 7.0
|
|
|
|
|
|
|
|
|
|
-- Record syntax also allows a simple form of update
|
|
|
|
|
|
|
|
|
|
myPoint' = myPoint { x = 9.0 }
|
|
|
|
|
|
|
|
|
|
-- myPoint' is CartesianPoint2D { x = 9.0, y = 10.0 }
|
|
|
|
|
|
|
|
|
|
-- Even if a type is defined with record syntax, it can be declared like
|
|
|
|
|
-- a simple data constructor. This is fine:
|
|
|
|
|
|
|
|
|
|
myPoint'2 = CartesianPoint2D 3.3 4.0
|
|
|
|
|
|
|
|
|
|
-- It's also useful to pattern match data constructors in `case` expressions
|
|
|
|
|
|
2019-11-04 17:05:21 +00:00
|
|
|
|
distanceFromOrigin x =
|
|
|
|
|
case x of (CartesianPoint2D x y) -> sqrt $ x ** 2 + y ** 2
|
|
|
|
|
(PolarPoint2D r _) -> r
|
2019-11-04 16:51:48 +00:00
|
|
|
|
|
|
|
|
|
-- Your data types can have type parameters too:
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
|
|
|
|
data Maybe a = Nothing | Just a
|
|
|
|
|
|
|
|
|
|
-- These are all of type Maybe
|
2013-07-04 06:12:53 +00:00
|
|
|
|
Just "hello" -- of type `Maybe String`
|
|
|
|
|
Just 1 -- of type `Maybe Int`
|
|
|
|
|
Nothing -- of type `Maybe a` for any `a`
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
2019-11-04 16:51:48 +00:00
|
|
|
|
-- For convenience we can also create type synonyms with the 'type' keyword
|
|
|
|
|
|
|
|
|
|
type String = [Char]
|
|
|
|
|
|
|
|
|
|
-- Unlike `data` types, type synonyms need no constructor, and can be used
|
|
|
|
|
-- anywhere a synonymous data type could be used. Say we have the
|
|
|
|
|
-- following type synonyms and items with the following type signatures
|
|
|
|
|
|
|
|
|
|
type Weight = Float
|
|
|
|
|
type Height = Float
|
|
|
|
|
type Point = (Float, Float)
|
|
|
|
|
getMyHeightAndWeight :: Person -> (Height, Weight)
|
|
|
|
|
findCenter :: Circle -> Point
|
|
|
|
|
somePerson :: Person
|
|
|
|
|
someCircle :: Circle
|
|
|
|
|
distance :: Point -> Point -> Float
|
|
|
|
|
|
2019-11-04 17:05:21 +00:00
|
|
|
|
-- The following would compile and run without issue,
|
|
|
|
|
-- even though it does not make sense semantically,
|
|
|
|
|
-- because the type synonyms reduce to the same base types
|
2019-11-04 16:51:48 +00:00
|
|
|
|
|
|
|
|
|
distance (getMyHeightAndWeight somePerson) (findCenter someCircle)
|
|
|
|
|
|
|
|
|
|
----------------------------------------------------
|
|
|
|
|
-- 8. Typeclasses
|
|
|
|
|
----------------------------------------------------
|
|
|
|
|
|
|
|
|
|
-- Typeclasses are one way Haskell does polymorphism
|
|
|
|
|
-- They are similar to interfaces in other languages
|
2019-11-04 17:05:21 +00:00
|
|
|
|
-- A typeclass defines a set of functions that must
|
|
|
|
|
-- work on any type that is in that typeclass.
|
2019-11-04 16:51:48 +00:00
|
|
|
|
|
2019-11-04 17:05:21 +00:00
|
|
|
|
-- The Eq typeclass is for types whose instances can
|
|
|
|
|
-- be tested for equality with one another.
|
2019-11-04 16:51:48 +00:00
|
|
|
|
|
|
|
|
|
class Eq a where
|
|
|
|
|
(==) :: a -> a -> Bool
|
|
|
|
|
(/=) :: a -> a -> Bool
|
|
|
|
|
x == y = not (x /= y)
|
|
|
|
|
x /= y = not (x == y)
|
|
|
|
|
|
|
|
|
|
-- This defines a typeclass that requires two functions, (==) and (/=)
|
|
|
|
|
-- It also declares that one function can be declared in terms of another
|
|
|
|
|
-- So it is enough that *either* the (==) function or the (/=) is defined
|
|
|
|
|
-- And the other will be 'filled in' based on the typeclass definition
|
|
|
|
|
|
|
|
|
|
-- To make a type a member of a type class, the instance keyword is used
|
|
|
|
|
|
|
|
|
|
instance Eq TrafficLight where
|
|
|
|
|
Red == Red = True
|
|
|
|
|
Green == Green = True
|
|
|
|
|
Yellow == Yellow = True
|
|
|
|
|
_ == _ = False
|
|
|
|
|
|
|
|
|
|
-- Now we can use (==) and (/=) with TrafficLight objects
|
|
|
|
|
|
|
|
|
|
canProceedThrough :: TrafficLight -> Bool
|
|
|
|
|
canProceedThrough t = t /= Red
|
|
|
|
|
|
|
|
|
|
-- You can NOT create an instance definition for a type synonym
|
|
|
|
|
|
2019-11-04 17:05:21 +00:00
|
|
|
|
-- Functions can be written to take typeclasses with type parameters,
|
|
|
|
|
-- rather than types, assuming that the function only relies on
|
|
|
|
|
-- features of the typeclass
|
2019-11-04 16:51:48 +00:00
|
|
|
|
|
|
|
|
|
isEqual (Eq a) => a -> a -> Bool
|
|
|
|
|
isEqual x y = x == y
|
|
|
|
|
|
2019-11-04 17:05:21 +00:00
|
|
|
|
-- Note that x and y MUST be the same type, as they are both defined
|
|
|
|
|
-- as being of type parameter 'a'.
|
|
|
|
|
-- A typeclass does not state that different types in the typeclass can
|
|
|
|
|
-- be mixed together.
|
|
|
|
|
-- So `isEqual Red 2` is invalid, even though 2 is an Int which is an
|
|
|
|
|
-- instance of Eq, and Red is a TrafficLight which is also an instance of Eq
|
2019-11-04 16:51:48 +00:00
|
|
|
|
|
|
|
|
|
-- Other common typeclasses are:
|
|
|
|
|
-- Ord for types that can be ordered, allowing you to use >, <=, etc.
|
|
|
|
|
-- Read for types that can be created from a string representation
|
|
|
|
|
-- Show for types that can be converted to a string for display
|
2019-11-04 17:05:21 +00:00
|
|
|
|
-- Num, Real, Integral, Fractional for types that can do math
|
2019-11-04 16:51:48 +00:00
|
|
|
|
-- Enum for types that can be stepped through
|
|
|
|
|
-- Bounded for types with a maximum and minimum
|
|
|
|
|
|
2019-11-04 17:05:21 +00:00
|
|
|
|
-- Haskell can automatically make types part of Eq, Ord, Read, Show, Enum,
|
|
|
|
|
-- and Bounded with the `deriving` keyword at the end of the type declaration
|
2019-11-04 16:51:48 +00:00
|
|
|
|
|
|
|
|
|
data Point = Point Float Float deriving (Eq, Read, Show)
|
|
|
|
|
|
|
|
|
|
-- In this case it is NOT necessary to create an 'instance' definition
|
|
|
|
|
|
2013-06-29 00:01:58 +00:00
|
|
|
|
----------------------------------------------------
|
2019-11-04 16:51:48 +00:00
|
|
|
|
-- 9. Haskell IO
|
2013-06-29 22:10:47 +00:00
|
|
|
|
----------------------------------------------------
|
|
|
|
|
|
2013-06-30 17:03:42 +00:00
|
|
|
|
-- While IO can't be explained fully without explaining monads,
|
|
|
|
|
-- it is not hard to explain enough to get going.
|
2013-06-29 22:10:47 +00:00
|
|
|
|
|
2014-02-03 23:09:52 +00:00
|
|
|
|
-- When a Haskell program is executed, `main` is
|
2015-10-23 21:31:10 +00:00
|
|
|
|
-- called. It must return a value of type `IO a` for some type `a`. For example:
|
2013-07-04 05:52:48 +00:00
|
|
|
|
|
|
|
|
|
main :: IO ()
|
2015-03-16 20:07:19 +00:00
|
|
|
|
main = putStrLn $ "Hello, sky! " ++ (say Blue)
|
2013-07-04 05:52:48 +00:00
|
|
|
|
-- putStrLn has type String -> IO ()
|
|
|
|
|
|
2015-03-16 20:07:19 +00:00
|
|
|
|
-- It is easiest to do IO if you can implement your program as
|
|
|
|
|
-- a function from String to String. The function
|
2013-07-04 05:52:48 +00:00
|
|
|
|
-- interact :: (String -> String) -> IO ()
|
2015-03-16 20:07:19 +00:00
|
|
|
|
-- inputs some text, runs a function on it, and prints out the
|
2013-07-04 05:52:48 +00:00
|
|
|
|
-- output.
|
|
|
|
|
|
|
|
|
|
countLines :: String -> String
|
|
|
|
|
countLines = show . length . lines
|
|
|
|
|
|
|
|
|
|
main' = interact countLines
|
|
|
|
|
|
|
|
|
|
-- You can think of a value of type `IO ()` as representing a
|
|
|
|
|
-- sequence of actions for the computer to do, much like a
|
|
|
|
|
-- computer program written in an imperative language. We can use
|
|
|
|
|
-- the `do` notation to chain actions together. For example:
|
|
|
|
|
|
|
|
|
|
sayHello :: IO ()
|
2015-03-16 20:07:19 +00:00
|
|
|
|
sayHello = do
|
2013-07-04 05:52:48 +00:00
|
|
|
|
putStrLn "What is your name?"
|
2013-09-20 05:25:36 +00:00
|
|
|
|
name <- getLine -- this gets a line and gives it the name "name"
|
2013-07-04 05:52:48 +00:00
|
|
|
|
putStrLn $ "Hello, " ++ name
|
2015-03-16 20:07:19 +00:00
|
|
|
|
|
2013-07-04 05:52:48 +00:00
|
|
|
|
-- Exercise: write your own version of `interact` that only reads
|
|
|
|
|
-- one line of input.
|
2015-03-16 20:07:19 +00:00
|
|
|
|
|
2013-07-04 05:52:48 +00:00
|
|
|
|
-- The code in `sayHello` will never be executed, however. The only
|
2015-03-16 20:07:19 +00:00
|
|
|
|
-- action that ever gets executed is the value of `main`.
|
|
|
|
|
-- To run `sayHello` comment out the above definition of `main`
|
2013-07-04 05:52:48 +00:00
|
|
|
|
-- and replace it with:
|
|
|
|
|
-- main = sayHello
|
|
|
|
|
|
2015-03-16 20:07:19 +00:00
|
|
|
|
-- Let's understand better how the function `getLine` we just
|
2013-07-04 05:52:48 +00:00
|
|
|
|
-- used works. Its type is:
|
|
|
|
|
-- getLine :: IO String
|
2013-07-04 06:12:53 +00:00
|
|
|
|
-- You can think of a value of type `IO a` as representing a
|
2015-03-16 20:07:19 +00:00
|
|
|
|
-- computer program that will generate a value of type `a`
|
2013-07-04 05:52:48 +00:00
|
|
|
|
-- when executed (in addition to anything else it does). We can
|
2015-10-23 21:31:10 +00:00
|
|
|
|
-- name and reuse this value using `<-`. We can also
|
2013-07-04 05:52:48 +00:00
|
|
|
|
-- make our own action of type `IO String`:
|
|
|
|
|
|
2013-06-30 17:03:42 +00:00
|
|
|
|
action :: IO String
|
2013-06-29 22:10:47 +00:00
|
|
|
|
action = do
|
|
|
|
|
putStrLn "This is a line. Duh"
|
2015-03-16 20:07:19 +00:00
|
|
|
|
input1 <- getLine
|
2013-06-29 22:10:47 +00:00
|
|
|
|
input2 <- getLine
|
2013-07-04 05:52:48 +00:00
|
|
|
|
-- The type of the `do` statement is that of its last line.
|
2015-03-16 20:07:19 +00:00
|
|
|
|
-- `return` is not a keyword, but merely a function
|
2013-07-04 05:52:48 +00:00
|
|
|
|
return (input1 ++ "\n" ++ input2) -- return :: String -> IO String
|
2013-06-29 22:10:47 +00:00
|
|
|
|
|
2013-07-04 05:52:48 +00:00
|
|
|
|
-- We can use this just like we used `getLine`:
|
2013-06-29 22:10:47 +00:00
|
|
|
|
|
2013-07-04 05:52:48 +00:00
|
|
|
|
main'' = do
|
|
|
|
|
putStrLn "I will echo two lines!"
|
2015-03-16 20:07:19 +00:00
|
|
|
|
result <- action
|
2013-06-29 22:10:47 +00:00
|
|
|
|
putStrLn result
|
|
|
|
|
putStrLn "This was all, folks!"
|
2013-06-30 17:03:42 +00:00
|
|
|
|
|
2013-07-04 06:01:47 +00:00
|
|
|
|
-- The type `IO` is an example of a "monad". The way Haskell uses a monad to
|
|
|
|
|
-- do IO allows it to be a purely functional language. Any function that
|
|
|
|
|
-- interacts with the outside world (i.e. does IO) gets marked as `IO` in its
|
2017-02-09 15:26:11 +00:00
|
|
|
|
-- type signature. This lets us reason about which functions are "pure" (don't
|
|
|
|
|
-- interact with the outside world or modify state) and which functions aren't.
|
2013-07-04 06:01:47 +00:00
|
|
|
|
|
|
|
|
|
-- This is a powerful feature, because it's easy to run pure functions
|
|
|
|
|
-- concurrently; so, concurrency in Haskell is very easy.
|
2013-06-29 22:10:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
----------------------------------------------------
|
2019-11-04 16:51:48 +00:00
|
|
|
|
-- 10. The Haskell REPL
|
2013-06-29 00:01:58 +00:00
|
|
|
|
----------------------------------------------------
|
|
|
|
|
|
|
|
|
|
-- Start the repl by typing `ghci`.
|
|
|
|
|
-- Now you can type in Haskell code. Any new values
|
|
|
|
|
-- need to be created with `let`:
|
|
|
|
|
|
|
|
|
|
let foo = 5
|
|
|
|
|
|
2015-10-31 12:37:13 +00:00
|
|
|
|
-- You can see the type of any value or expression with `:t`:
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
2015-10-31 12:37:13 +00:00
|
|
|
|
> :t foo
|
2013-06-29 00:01:58 +00:00
|
|
|
|
foo :: Integer
|
2013-07-04 05:52:48 +00:00
|
|
|
|
|
2015-10-31 12:37:13 +00:00
|
|
|
|
-- Operators, such as `+`, `:` and `$`, are functions.
|
|
|
|
|
-- Their type can be inspected by putting the operator in parentheses:
|
|
|
|
|
|
|
|
|
|
> :t (:)
|
|
|
|
|
(:) :: a -> [a] -> [a]
|
|
|
|
|
|
|
|
|
|
-- You can get additional information on any `name` using `:i`:
|
|
|
|
|
|
|
|
|
|
> :i (+)
|
|
|
|
|
class Num a where
|
|
|
|
|
(+) :: a -> a -> a
|
|
|
|
|
...
|
|
|
|
|
-- Defined in ‘GHC.Num’
|
|
|
|
|
infixl 6 +
|
|
|
|
|
|
2013-07-04 05:52:48 +00:00
|
|
|
|
-- You can also run any action of type `IO ()`
|
|
|
|
|
|
|
|
|
|
> sayHello
|
|
|
|
|
What is your name?
|
|
|
|
|
Friend!
|
|
|
|
|
Hello, Friend!
|
|
|
|
|
|
2013-06-29 00:01:58 +00:00
|
|
|
|
```
|
|
|
|
|
|
2013-12-02 12:09:58 +00:00
|
|
|
|
There's a lot more to Haskell, including typeclasses and monads. These are the
|
|
|
|
|
big ideas that make Haskell such fun to code in. I'll leave you with one final
|
2015-10-23 21:31:10 +00:00
|
|
|
|
Haskell example: an implementation of a quicksort variant in Haskell:
|
2013-06-29 00:01:58 +00:00
|
|
|
|
|
|
|
|
|
```haskell
|
|
|
|
|
qsort [] = []
|
|
|
|
|
qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater
|
|
|
|
|
where lesser = filter (< p) xs
|
|
|
|
|
greater = filter (>= p) xs
|
|
|
|
|
```
|
2013-06-29 00:09:34 +00:00
|
|
|
|
|
2015-12-03 22:27:24 +00:00
|
|
|
|
There are two popular ways to install Haskell: The traditional [Cabal-based installation](http://www.haskell.org/platform/), and the newer [Stack-based process](https://www.stackage.org/install).
|
2013-06-29 03:53:43 +00:00
|
|
|
|
|
2013-07-09 14:42:37 +00:00
|
|
|
|
You can find a much gentler introduction from the excellent
|
2017-09-10 19:26:33 +00:00
|
|
|
|
[Learn you a Haskell](http://learnyouahaskell.com/),
|
|
|
|
|
[Happy Learn Haskell Tutorial](http://www.happylearnhaskelltutorial.com/) or
|
2013-07-09 14:42:37 +00:00
|
|
|
|
[Real World Haskell](http://book.realworldhaskell.org/).
|