From 7e4bd6d17e2003b0f5fd2fe6859c722a72c4acc3 Mon Sep 17 00:00:00 2001 From: noahlz Date: Sat, 29 Jun 2013 17:17:52 -0400 Subject: Fix issue #50 --- haskell.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'haskell.html.markdown') diff --git a/haskell.html.markdown b/haskell.html.markdown index 563674c9..45cfb819 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -104,6 +104,10 @@ snd ("haskell", 1) -- 1 -- A simple function that takes two variables add a b = a + b +-- Note that if you are using ghci (the Haskell interpreter) +-- You'll need to use `let`, i.e. +-- let add a b = a + b + -- Using the function add 1 2 -- 3 -- cgit v1.2.3 From 59d801c0dfda9a7ddf27261d8a6f7906b049de0a Mon Sep 17 00:00:00 2001 From: DR6 Date: Sat, 29 Jun 2013 23:10:47 +0100 Subject: Added IO to haskell's tutorial --- haskell.html.markdown | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'haskell.html.markdown') diff --git a/haskell.html.markdown b/haskell.html.markdown index 563674c9..fbaa93f2 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -259,7 +259,32 @@ Just "hello" Just 1 ---------------------------------------------------- --- 8. The Haskell REPL +-- 8. Haskell IO +---------------------------------------------------- + +-- While IO can't be explained fully without explaining monads +-- it is not hard to explain enough to get going + +-- An IO a value is an IO action: you can chain them with do blocks +action = do + putStrLn "This is a line. Duh" + input <- getLine -- this gets a line and gives it the name "input" + input2 <- getLine + return (input1++"\n"++input2) -- This is the result of the whole action + +-- This didn't actually do anything. When a haskell program is executed +-- an IO action called "main" is read and interprete + +main = do + putStrLn "Our first program. How exciting!" + result <- action -- our defined action is just like the default ones + putStrLn result + putStrLn "This was all, folks!" + + + +---------------------------------------------------- +-- 9. The Haskell REPL ---------------------------------------------------- -- Start the repl by typing `ghci`. -- cgit v1.2.3 From 83aeecb68a20751d09bb83793691f19a8dc97aa2 Mon Sep 17 00:00:00 2001 From: Adam Date: Sat, 29 Jun 2013 20:19:14 -0700 Subject: Added filename parameter --- haskell.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'haskell.html.markdown') diff --git a/haskell.html.markdown b/haskell.html.markdown index fbaa93f2..a696cb5f 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -2,6 +2,7 @@ language: haskell author: Adit Bhargava author_url: http://adit.io +filename: learnhaskell.hs --- Haskell was designed as a practical, purely functional programming language. It's famous for -- cgit v1.2.3 From 7ee4774f9b857f4d17fdcb086130bca862ae15f0 Mon Sep 17 00:00:00 2001 From: Aditya Bhargava Date: Sun, 30 Jun 2013 10:03:42 -0700 Subject: fixing issues listed in "gripes about haskell" (fixes bug #45) --- haskell.html.markdown | 66 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 18 deletions(-) (limited to 'haskell.html.markdown') diff --git a/haskell.html.markdown b/haskell.html.markdown index a696cb5f..7158e2aa 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -45,15 +45,21 @@ not False -- True 1 /= 1 -- False 1 < 10 -- True +-- 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. + -- Strings and characters "This is a string." 'a' -- character 'You cant use single quotes for strings.' -- error! --- Strings can be added too! +-- Strings can be concatenated "Hello " ++ "world!" -- "Hello world!" --- A string can be treated like a list of characters +-- A string is a list of characters "This is a string" !! 0 -- 'T' @@ -69,14 +75,24 @@ not False -- True -- You can also have infinite lists in Haskell! [1..] -- a list of all the natural numbers --- joining two lists +-- 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. + +- 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 -- 4 +[0..] !! 5 -- 5 -- more list operations head [1..5] -- 1 @@ -136,12 +152,12 @@ foo (x, y) = (x + 1, y + 2) -- Pattern matching on arrays. Here `x` is the first element -- in the array, and `xs` is the rest of the array. We can write -- our own map function: -map func [x] = [func x] -map func (x:xs) = func x:(map func xs) +myMap func [x] = [func x] +myMap func (x:xs) = func x:(myMap func xs) -- Anonymous functions are created with a backslash followed by -- all the arguments. -map (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7] +myMap (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7] -- using fold (called `inject` in some languages) with an anonymous -- function. foldl1 means fold left, and use the first value in the @@ -180,10 +196,10 @@ foo 5 -- 75 -- of parentheses: -- before -(even (double 7)) -- true +(even (fib 7)) -- true -- after -even . double $ 7 -- true +even . fib $ 7 -- true ---------------------------------------------------- -- 5. Type signatures @@ -198,13 +214,17 @@ True :: Bool -- Functions have types too. -- `not` takes a boolean and returns a boolean: -not :: Bool -> Bool +-- not :: Bool -> Bool -- Here's a function that takes two arguments: -add :: Integer -> Integer -> Integer +-- add :: Integer -> Integer -> Integer + +-- When you define a value, it's good practice to write it's type above it: +double :: Integer -> Integer +double x = x * 2 ---------------------------------------------------- --- 6. Control Flow +-- 6. Control Flow and If Statements ---------------------------------------------------- -- if statements @@ -263,25 +283,35 @@ Just 1 -- 8. Haskell IO ---------------------------------------------------- --- While IO can't be explained fully without explaining monads --- it is not hard to explain enough to get going +-- While IO can't be explained fully without explaining monads, +-- it is not hard to explain enough to get going. --- An IO a value is an IO action: you can chain them with do blocks +-- An `IO a` value is an IO action: you can chain them with do blocks +action :: IO String action = do putStrLn "This is a line. Duh" input <- getLine -- this gets a line and gives it the name "input" input2 <- getLine - return (input1++"\n"++input2) -- This is the result of the whole action + return (input1 ++ "\n" ++ input2) -- This is the result of the whole action -- This didn't actually do anything. When a haskell program is executed --- an IO action called "main" is read and interprete +-- an IO action called "main" is read and interpreted. main = do putStrLn "Our first program. How exciting!" result <- action -- our defined action is just like the default ones putStrLn result putStrLn "This was all, folks!" - + +-- Haskell does IO through a monad because this allows it to be a purely +-- functional language. Our `action` function had a type signature of `IO String`. +-- In general any function that interacts with the outside world (i.e. does IO) +-- gets marked as `IO` in it's 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. + +-- This is a powerful feature, because it's easy to run pure functions concurrently +-- so concurrency in Haskell is very easy. ---------------------------------------------------- -- cgit v1.2.3 From bc0f1fca6c5f248257993e7bc80bf2d54652b7aa Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 30 Jun 2013 14:50:51 -0700 Subject: Removed filename from haskell tutorial (file is not executable) --- haskell.html.markdown | 1 - 1 file changed, 1 deletion(-) (limited to 'haskell.html.markdown') diff --git a/haskell.html.markdown b/haskell.html.markdown index 7158e2aa..2437ceed 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -2,7 +2,6 @@ language: haskell author: Adit Bhargava author_url: http://adit.io -filename: learnhaskell.hs --- Haskell was designed as a practical, purely functional programming language. It's famous for -- cgit v1.2.3 From 9b6c837916dc50a4646ae1666528681e6e4a881c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Mon, 1 Jul 2013 17:33:25 +0200 Subject: Remove mentions of array Since all the functions work with lists, calling them arrays is inaccurate. This commit also updates `myMap` function so that it works on an empty list as well as resolves ambiguity about `x` from comment. --- haskell.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'haskell.html.markdown') diff --git a/haskell.html.markdown b/haskell.html.markdown index f3baa9a5..1a4cdc67 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -152,10 +152,10 @@ fib x = fib (x - 1) + fib (x - 2) -- Pattern matching on tuples: foo (x, y) = (x + 1, y + 2) --- Pattern matching on arrays. Here `x` is the first element --- in the array, and `xs` is the rest of the array. We can write +-- Pattern matching on lists. Here `x` is the first element +-- in the list, and `xs` is the rest of the list. We can write -- our own map function: -myMap func [x] = [func x] +myMap func [] = [] myMap func (x:xs) = func x:(myMap func xs) -- Anonymous functions are created with a backslash followed by @@ -164,7 +164,7 @@ myMap (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7] -- using fold (called `inject` in some languages) with an anonymous -- function. foldl1 means fold left, and use the first value in the --- array as the initial value for the accumulator. +-- list as the initial value for the accumulator. foldl1 (\acc x -> acc + x) [1..5] -- 15 ---------------------------------------------------- -- cgit v1.2.3 From d553ee4c6bf17f14acf06471963859326f8a5f8b Mon Sep 17 00:00:00 2001 From: elisee Date: Mon, 1 Jul 2013 17:50:25 +0200 Subject: Fix various "it's" -> "its" for Haskell doc --- 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 1a4cdc67..84b8f263 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -5,7 +5,7 @@ author_url: http://adit.io --- Haskell was designed as a practical, purely functional programming language. It's famous for -it's monads and it's type system, but I keep coming back to it because of it's elegance. Haskell +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 @@ -222,7 +222,7 @@ True :: Bool -- Here's a function that takes two arguments: -- add :: Integer -> Integer -> Integer --- When you define a value, it's good practice to write it's type above it: +-- When you define a value, it's good practice to write its type above it: double :: Integer -> Integer double x = x * 2 @@ -309,7 +309,7 @@ main = do -- Haskell does IO through a monad because this allows it to be a purely -- functional language. Our `action` function had a type signature of `IO String`. -- In general any function that interacts with the outside world (i.e. does IO) --- gets marked as `IO` in it's type signature. This lets us reason about what +-- 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. -- cgit v1.2.3