From 5f45319d577e1ef8a0d28f08fd37a7ed76433958 Mon Sep 17 00:00:00 2001 From: Devin McGinty Date: Sat, 31 Jan 2015 19:40:55 -0500 Subject: Add more information on ranges in Haskell --- haskell.html.markdown | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'haskell.html.markdown') diff --git a/haskell.html.markdown b/haskell.html.markdown index 748a29da..d5dd5141 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -67,10 +67,18 @@ 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] + -- You can also have infinite lists in Haskell! [1..] -- a list of all the natural numbers -- cgit v1.2.3 From 411b2b01d3a83369f0754ec03b40499e4f8bcade Mon Sep 17 00:00:00 2001 From: Devin McGinty Date: Sun, 1 Feb 2015 16:39:52 -0500 Subject: Add example of string-as-list and fix punctuation --- haskell.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'haskell.html.markdown') diff --git a/haskell.html.markdown b/haskell.html.markdown index d5dd5141..52433aaa 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -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,7 +68,7 @@ not False -- True ---------------------------------------------------- -- Every element in a list must have the same type. --- These two lists are the same +-- These two lists are the same: [1, 2, 3, 4, 5] [1..5] -- cgit v1.2.3 From 36a296de5d8d82212755ba33388517486f7f804c Mon Sep 17 00:00:00 2001 From: Zach Munro-Cape Date: Sun, 1 Mar 2015 15:16:29 -0400 Subject: Moved explanation of indexing into a list earlier This way, it is clear what [1..] !! 999 means later on. --- haskell.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'haskell.html.markdown') diff --git a/haskell.html.markdown b/haskell.html.markdown index 52433aaa..79fbf09f 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -80,6 +80,9 @@ not False -- True [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 @@ -99,9 +102,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] -- cgit v1.2.3 From 4e00fa4734bd39436f3336b4ce1034b65c265657 Mon Sep 17 00:00:00 2001 From: Cornel Punga Date: Mon, 16 Mar 2015 21:09:17 +0200 Subject: Update haskell.html.markdown. Wrong explanation about '$' operator --- haskell.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'haskell.html.markdown') diff --git a/haskell.html.markdown b/haskell.html.markdown index 79fbf09f..d5dfd122 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -202,9 +202,9 @@ 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 `$` (often in combination with `.`) +-- Haskell has another function called `$`. Anything appearing after it will +-- take precedence over anything that comes before. +-- You can use `$` (often in combination with `.`) -- to get rid of a lot of parentheses: -- before -- cgit v1.2.3 From 9fb21f1ce4c02e8cad50046f90d026ccab284626 Mon Sep 17 00:00:00 2001 From: Cornel Punga Date: Mon, 16 Mar 2015 21:24:21 +0200 Subject: Haskell.html.markdown. Wrong result of an expression. The seventh Fibonacci number is 13 (odd number), so the result of 'even 13' is false. --- haskell.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'haskell.html.markdown') diff --git a/haskell.html.markdown b/haskell.html.markdown index d5dfd122..f1025d44 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -208,13 +208,13 @@ foo 5 -- 75 -- to get rid of a lot of parentheses: -- before -(even (fib 7)) -- true +(even (fib 7)) -- false -- after -even . fib $ 7 -- true +even . fib $ 7 -- false -- equivalently -even $ fib 7 -- true +even $ fib 7 -- false ---------------------------------------------------- -- 5. Type signatures -- cgit v1.2.3 From a81affb3027b81aa16eba8cac159a18081cff449 Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Mon, 16 Mar 2015 14:07:19 -0600 Subject: Make the two fib functions consistent --- haskell.html.markdown | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'haskell.html.markdown') diff --git a/haskell.html.markdown b/haskell.html.markdown index 79fbf09f..2f807c5f 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -148,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) @@ -181,7 +181,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 @@ -319,13 +319,13 @@ Nothing -- of type `Maybe a` for any `a` -- 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 @@ -339,43 +339,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 "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!" -- cgit v1.2.3 From 2f43da109ffab5a4d3dd582cc51a7c3d95dd4987 Mon Sep 17 00:00:00 2001 From: Cornel Punga Date: Wed, 25 Mar 2015 18:08:32 +0200 Subject: [haskell.html.markdown] Changed explanation for Haskell '$' operator --- haskell.html.markdown | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'haskell.html.markdown') diff --git a/haskell.html.markdown b/haskell.html.markdown index f1025d44..6bdc78e0 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -202,10 +202,12 @@ foo = (*5) . (+10) foo 5 -- 75 -- fixing precedence --- Haskell has another function called `$`. Anything appearing after it will --- take precedence over anything that comes before. --- You can use `$` (often in combination with `.`) --- 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 +-- all other operators on both sides of `$` will be evaluated before applying +-- the `$`. -- before (even (fib 7)) -- false -- cgit v1.2.3 From e8a1ee8912d1c9bb0145c2afbdc530463fc612b7 Mon Sep 17 00:00:00 2001 From: Cornel Punga Date: Fri, 27 Mar 2015 15:25:33 +0200 Subject: [haskell.html.markdown] Explanation for Haskell '$' operator with support from @geoffliu --- haskell.html.markdown | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'haskell.html.markdown') diff --git a/haskell.html.markdown b/haskell.html.markdown index 6bdc78e0..6c98c2b6 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -205,9 +205,8 @@ foo 5 -- 75 -- 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 --- all other operators on both sides of `$` will be evaluated before applying --- the `$`. +-- 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)) -- false -- cgit v1.2.3 From 745ce28d7e72a1de6a1a386f91f8fcae0c99b324 Mon Sep 17 00:00:00 2001 From: Tiddo Langerak Date: Mon, 27 Apr 2015 10:16:16 +0200 Subject: Fixed haskell foldr example For #1068 --- haskell.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'haskell.html.markdown') diff --git a/haskell.html.markdown b/haskell.html.markdown index f28fcfe7..6a64442f 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -282,7 +282,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 -- cgit v1.2.3