diff options
| author | Suzane Sant Ana <tetestonaldo@gmail.com> | 2017-12-31 14:27:06 -0200 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-12-31 14:27:06 -0200 | 
| commit | 42f9329bb3a028d374d6397991ac48b44064741e (patch) | |
| tree | 1e75e2b3e122aeb863e3ffa037f6f64c4027fbf8 /haskell.html.markdown | |
| parent | e6b77595f2669d66ac7be43c6e6083cbff80a9a7 (diff) | |
| parent | 70a36c9bd970b928adde06afb2bd69f6ba8e5d5c (diff) | |
Merge pull request #1 from adambard/master
update
Diffstat (limited to 'haskell.html.markdown')
| -rw-r--r-- | haskell.html.markdown | 141 | 
1 files changed, 84 insertions, 57 deletions
| diff --git a/haskell.html.markdown b/haskell.html.markdown index 748a29da..266cf11b 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -1,5 +1,6 @@  ---  language: Haskell +filename: learnhaskell.hs  contributors:      - ["Adit Bhargava", "http://adit.io"]  --- @@ -59,18 +60,30 @@ 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'  ---------------------------------------------------- --- Lists and Tuples +-- 2. Lists and Tuples  ----------------------------------------------------  -- Every element in a list must have the same type. --- Two lists that are the same +-- These two lists are equal:  [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] -- [] (Haskell defaults to incrementing) +[5,4..1] -- [5, 4, 3, 2, 1] + +-- indexing into a list +[1..10] !! 3 -- 4 (zero-based indexing) +  -- You can also have infinite lists in Haskell!  [1..] -- a list of all the natural numbers @@ -90,9 +103,6 @@ not False -- True  -- 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] @@ -139,12 +149,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.  +-- 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) @@ -172,7 +182,7 @@ foldl1 (\acc x -> acc + x) [1..5] -- 15  ----------------------------------------------------  -- 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  +-- it gets "partially applied". That means it returns a function that takes the  -- rest of the arguments.  add a b = a + b @@ -180,38 +190,40 @@ 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 (.) function chains functions together. +-- 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 5, and then returns the final value. -foo = (*5) . (+10) +-- multiplies the result of that by 4, and then returns the final value. +foo = (4*) . (10+) --- (5 + 10) * 5 = 75 -foo 5 -- 75 +-- 4*(10+5) = 60 +foo 5 -- 60  -- 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 `$` (often in combination with `.`) --- to get rid of a lot of parentheses: +-- 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 +-- the expression on its right is applied as the parameter to the function on its left.  -- before -(even (fib 7)) -- true - --- after -even . fib $ 7 -- true +even (fib 7) -- false  -- equivalently -even $ fib 7 -- true +even $ fib 7 -- false + +-- composing functions +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 @@ -233,10 +245,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" @@ -248,7 +260,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] @@ -268,11 +280,11 @@ 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 -(2 * 3 + (2 * 2 + (2 * 1 + 4))) +(2 * 1 + (2 * 2 + (2 * 3 + 4)))  ----------------------------------------------------  -- 7. Data Types @@ -284,11 +296,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: @@ -307,16 +318,16 @@ 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)  +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 @@ -330,51 +341,51 @@ 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 "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  +-- name 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!"  -- 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. @@ -390,11 +401,26 @@ main'' = do  let foo = 5 --- You can see the type of any value with `:t`: +-- You can see the type of any value or expression with `:t`: ->:t foo +> :t foo  foo :: Integer +-- 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 + +  -- You can also run any action of type `IO ()`  > sayHello @@ -406,7 +432,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 [] = [] @@ -415,8 +441,9 @@ qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater            greater = filter (>= p) xs  ``` -Haskell is easy to install. Get it [here](http://www.haskell.org/platform/). +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).  You can find a much gentler introduction from the excellent -[Learn you a Haskell](http://learnyouahaskell.com/) or +[Learn you a Haskell](http://learnyouahaskell.com/), +[Happy Learn Haskell Tutorial](http://www.happylearnhaskelltutorial.com/) or  [Real World Haskell](http://book.realworldhaskell.org/). | 
