diff options
Diffstat (limited to 'haskell.html.markdown')
-rw-r--r-- | haskell.html.markdown | 110 |
1 files changed, 63 insertions, 47 deletions
diff --git a/haskell.html.markdown b/haskell.html.markdown index be7d8669..369b1b20 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -1,17 +1,17 @@ --- -language: haskell +language: Haskell contributors: - ["Adit Bhargava", "http://adit.io"] --- -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. +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. ```haskell -- Single line comments start with two dashes. {- Multiline comments can be enclosed -en a block like this. +in a block like this. -} ---------------------------------------------------- @@ -59,6 +59,7 @@ not False -- True "Hello " ++ "world!" -- "Hello world!" -- A string is a list of characters +['H', 'e', 'l', 'l', 'o'] -- "Hello" "This is a string" !! 0 -- 'T' @@ -67,10 +68,21 @@ not False -- True ---------------------------------------------------- -- Every element in a list must have the same type. --- Two lists that are the same +-- These two lists are the same: [1, 2, 3, 4, 5] [1..5] +-- Ranges are versatile. +['A'..'F'] -- "ABCDEF" + +-- 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,4..1] -- [5, 4, 3, 2, 1] + +-- indexing into a list +[0..] !! 5 -- 5 + -- You can also have infinite lists in Haskell! [1..] -- a list of all the natural numbers @@ -84,15 +96,12 @@ not False -- True -- rest of the elements of this "infinite" list don't exist yet! Haskell won't -- actually evaluate them until it needs to. -- joining two lists +-- joining two lists [1..5] ++ [6..10] -- adding to the head of a list 0:[1..5] -- [0, 1, 2, 3, 4, 5] --- indexing into a list -[0..] !! 5 -- 5 - -- more list operations head [1..5] -- 1 tail [1..5] -- [2, 3, 4, 5] @@ -110,7 +119,7 @@ last [1..5] -- 5 -- A tuple: ("haskell", 1) --- accessing elements of a tuple +-- accessing elements of a pair (i.e. a tuple of length 2) fst ("haskell", 1) -- "haskell" snd ("haskell", 1) -- 1 @@ -131,7 +140,7 @@ add 1 2 -- 3 -- with backticks: 1 `add` 2 -- 3 --- You can also define functions that have no characters! This lets +-- You can also define functions that have no letters! This lets -- you define your own operators! Here's an operator that does -- integer division (//) a b = a `div` b @@ -139,12 +148,12 @@ add 1 2 -- 3 -- Guards: an easy way to do branching in functions fib x - | x < 2 = x + | x < 2 = 1 | 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. +-- function that matches the pattern of the value. fib 1 = 1 fib 2 = 2 fib x = fib (x - 1) + fib (x - 2) @@ -171,8 +180,8 @@ foldl1 (\acc x -> acc + x) [1..5] -- 15 -- 4. More functions ---------------------------------------------------- --- currying: if you don't pass in all the arguments to a function, --- it gets "curried". That means it returns a function that takes the +-- partial application: if you don't pass in all the arguments to a function, +-- it gets "partially applied". That means it returns a function that takes the -- rest of the arguments. add a b = a + b @@ -193,16 +202,21 @@ foo = (*5) . (+10) foo 5 -- 75 -- fixing precedence --- Haskell has another function called `$`. This changes the precedence --- so that everything to the left of it gets computed first and then applied --- to everything on the right. You can use `.` and `$` to get rid of a lot --- of parentheses: +-- Haskell has another 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 +-- the expression on its right is applied as the parameter to the function on its left. -- before -(even (fib 7)) -- true +even (fib 7) -- false + +-- equivalently +even $ fib 7 -- false + +-- composing functions +even . fib $ 7 -- false --- after -even . fib $ 7 -- true ---------------------------------------------------- -- 5. Type signatures @@ -227,24 +241,24 @@ double :: Integer -> Integer double x = x * 2 ---------------------------------------------------- --- 6. Control Flow and If Statements +-- 6. Control Flow and If Expressions ---------------------------------------------------- --- if statements +-- if expressions haskell = if 1 == 1 then "awesome" else "awful" -- haskell = "awesome" --- if statements 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" --- case statements: Here's how you could parse command line arguments +-- case expressions: Here's how you could parse command line arguments case args of "help" -> printHelp "start" -> startProgram _ -> putStrLn "bad args" --- Haskell doesn't have loops because it uses recursion instead. +-- Haskell doesn't have loops; it uses recursion instead. -- map applies a function over every element in an array map (*2) [1..5] -- [2, 4, 6, 8, 10] @@ -269,7 +283,7 @@ foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43 foldr (\x y -> 2*x + y) 4 [1,2,3] -- 16 -- This is now the same as -(2 * 3 + (2 * 2 + (2 * 1 + 4))) +(2 * 1 + (2 * 2 + (2 * 3 + 4))) ---------------------------------------------------- -- 7. Data Types @@ -303,17 +317,17 @@ Nothing -- of type `Maybe a` for any `a` -- While IO can't be explained fully without explaining monads, -- it is not hard to explain enough to get going. --- When a Haskell program is executed, the function `main` is +-- When a Haskell program is executed, `main` is -- called. It must return a value of type `IO ()`. For example: main :: IO () -main = putStrLn $ "Hello, sky! " ++ (say Blue) +main = putStrLn $ "Hello, sky! " ++ (say Blue) -- putStrLn has type String -> IO () --- It is easiest to do IO if you can implement your program as --- a function from String to String. The function +-- It is easiest to do IO if you can implement your program as +-- a function from String to String. The function -- interact :: (String -> String) -> IO () --- inputs some text, runs a function on it, and prints out the +-- inputs some text, runs a function on it, and prints out the -- output. countLines :: String -> String @@ -327,43 +341,43 @@ main' = interact countLines -- the `do` notation to chain actions together. For example: sayHello :: IO () -sayHello = do +sayHello = do putStrLn "What is your name?" - name <- getLine -- this gets a line and gives it the name "input" + name <- getLine -- this gets a line and gives it the name "name" putStrLn $ "Hello, " ++ name - + -- Exercise: write your own version of `interact` that only reads -- one line of input. - + -- The code in `sayHello` will never be executed, however. The only --- action that ever gets executed is the value of `main`. --- To run `sayHello` comment out the above definition of `main` +-- action that ever gets executed is the value of `main`. +-- To run `sayHello` comment out the above definition of `main` -- and replace it with: -- main = sayHello --- Let's understand better how the function `getLine` we just +-- Let's understand better how the function `getLine` we just -- used works. Its type is: -- getLine :: IO String -- You can think of a value of type `IO a` as representing a --- computer program that will generate a value of type `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 +-- store and reuse this value using `<-`. We can also -- make our own action of type `IO String`: action :: IO String action = do putStrLn "This is a line. Duh" - input1 <- getLine + input1 <- getLine input2 <- getLine -- The type of the `do` statement is that of its last line. - -- `return` is not a keyword, but merely a function + -- `return` is not a keyword, but merely a function return (input1 ++ "\n" ++ input2) -- return :: String -> IO String -- We can use this just like we used `getLine`: main'' = do putStrLn "I will echo two lines!" - result <- action + result <- action putStrLn result putStrLn "This was all, folks!" @@ -401,7 +415,9 @@ 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: +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 qsort [] = [] |