summaryrefslogtreecommitdiffhomepage
path: root/haskell.html.markdown
diff options
context:
space:
mode:
authorhyphz <drmoose94@gmail.com>2017-07-18 17:56:42 +0100
committerhyphz <drmoose94@gmail.com>2017-07-18 17:56:42 +0100
commit5ab5cb9800822d607be2c6ac943377811db98158 (patch)
tree3c804707822744c20da1de54ff60fc8c3197781b /haskell.html.markdown
parent62102d02992f83b3a1fb745a39f36332dd4435b7 (diff)
parent6e7c5c793327f4a63b13e555894597915ca91fda (diff)
Merge remote-tracking branch 'adambard/master'
Diffstat (limited to 'haskell.html.markdown')
-rw-r--r--haskell.html.markdown47
1 files changed, 23 insertions, 24 deletions
diff --git a/haskell.html.markdown b/haskell.html.markdown
index 200e2538..4e254070 100644
--- a/haskell.html.markdown
+++ b/haskell.html.markdown
@@ -64,11 +64,11 @@ not False -- True
----------------------------------------------------
--- Lists and Tuples
+-- 2. Lists and Tuples
----------------------------------------------------
-- Every element in a list must have the same type.
--- These two lists are the same:
+-- These two lists are equal:
[1, 2, 3, 4, 5]
[1..5]
@@ -77,11 +77,11 @@ not False -- True
-- You can create a step in a range.
[0,2..10] -- [0, 2, 4, 6, 8, 10]
-[5..1] -- This doesn't work because Haskell defaults to incrementing.
+[5..1] -- [] (Haskell defaults to incrementing)
[5,4..1] -- [5, 4, 3, 2, 1]
-- indexing into a list
-[1..10] !! 3 -- 4
+[1..10] !! 3 -- 4 (zero-based indexing)
-- You can also have infinite lists in Haskell!
[1..] -- a list of all the natural numbers
@@ -152,8 +152,8 @@ fib x
| otherwise = fib (x - 1) + fib (x - 2)
-- Pattern matching is similar. Here we have given three different
--- definitions for fib. Haskell will automatically call the first
--- function that matches the pattern of the value.
+-- equations that define fib. Haskell will automatically use the first
+-- equation whose left hand side pattern matches the value.
fib 1 = 1
fib 2 = 2
fib x = fib (x - 1) + fib (x - 2)
@@ -189,20 +189,20 @@ 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
-foo = (+10)
+foo = (10+)
foo 5 -- 15
-- function composition
-- the operator `.` chains functions together.
-- For example, here foo is a function that takes a value. It adds 10 to it,
-- multiplies the result of that by 4, and then returns the final value.
-foo = (*4) . (+10)
+foo = (4*) . (10+)
--- (5 + 10) * 4 = 60
+-- 4*(10+5) = 60
foo 5 -- 60
-- fixing precedence
--- Haskell has another operator called `$`. This operator applies a function
+-- 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
-- has priority of 0 and is right-associative. Such a low priority means that
@@ -222,7 +222,7 @@ even . fib $ 7 -- false
-- 5. Type signatures
----------------------------------------------------
--- Haskell has a very strong type system, and everything has a type signature.
+-- Haskell has a very strong type system, and every valid expression has a type.
-- Some basic types:
5 :: Integer
@@ -244,10 +244,10 @@ double x = x * 2
-- 6. Control Flow and If Expressions
----------------------------------------------------
--- if expressions
+-- if-expressions
haskell = if 1 == 1 then "awesome" else "awful" -- haskell = "awesome"
--- if expressions can be on multiple lines too, indentation is important
+-- if-expressions can be on multiple lines too, indentation is important
haskell = if 1 == 1
then "awesome"
else "awful"
@@ -259,7 +259,7 @@ case args of
_ -> putStrLn "bad args"
-- Haskell doesn't have loops; it uses recursion instead.
--- map applies a function over every element in an array
+-- map applies a function over every element in a list
map (*2) [1..5] -- [2, 4, 6, 8, 10]
@@ -279,7 +279,7 @@ foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43
-- This is the same as
(2 * (2 * (2 * 4 + 1) + 2) + 3)
--- foldl is left-handed, foldr is right-
+-- foldl is left-handed, foldr is right-handed
foldr (\x y -> 2*x + y) 4 [1,2,3] -- 16
-- This is now the same as
@@ -295,11 +295,10 @@ data Color = Red | Blue | Green
-- Now you can use it in a function:
-
say :: Color -> String
-say Red = "You are Red!"
-say Blue = "You are Blue!"
-say Green = "You are Green!"
+say Red = "You are Red!"
+say Blue = "You are Blue!"
+say Green = "You are Green!"
-- Your data types can have parameters too:
@@ -318,7 +317,7 @@ Nothing -- of type `Maybe a` for any `a`
-- it is not hard to explain enough to get going.
-- When a Haskell program is executed, `main` is
--- called. It must return a value of type `IO ()`. For example:
+-- called. It must return a value of type `IO a` for some type `a`. For example:
main :: IO ()
main = putStrLn $ "Hello, sky! " ++ (say Blue)
@@ -361,7 +360,7 @@ sayHello = do
-- You can think of a value of type `IO a` as representing a
-- computer program that will generate a value of type `a`
-- when executed (in addition to anything else it does). We can
--- store and reuse this value using `<-`. We can also
+-- name and reuse this value using `<-`. We can also
-- make our own action of type `IO String`:
action :: IO String
@@ -384,8 +383,8 @@ main'' = do
-- 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
--- type signature. This lets us reason about what functions are "pure" (don't
--- interact with the outside world or modify state) and what functions aren't.
+-- 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.
-- This is a powerful feature, because it's easy to run pure functions
-- concurrently; so, concurrency in Haskell is very easy.
@@ -432,7 +431,7 @@ Hello, Friend!
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
-Haskell example: an implementation of quicksort in Haskell:
+Haskell example: an implementation of a quicksort variant in Haskell:
```haskell
qsort [] = []