From 6f255a5e62ead8d40c4d254dcc63488e543e68d8 Mon Sep 17 00:00:00 2001 From: Brian Rue Date: Fri, 28 Jun 2013 15:55:23 -0700 Subject: Use consistent example for demonstrating integer vs. float division I had to do a decent amount of mental math to figure out the old example (11 / 4 => 2). This one (5 / 2 => 2) is easier, and makes the difference vs. float division (5.0 / 2.0 => 2.5) easier to spot. --- python.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python.html.markdown b/python.html.markdown index a599f5d3..20e4f3a8 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -32,7 +32,7 @@ to Python 2.x. Look for another tour of Python 3 soon! # Division is a bit tricky. It is integer division and floors the results # automatically. -11 / 4 #=> 2 +5 / 2 #=> 2 # To fix division we need to learn about floats. 2.0 # This is a float -- cgit v1.2.3 From cf98f74d6147e1aded5a863298f9cb1446d5da23 Mon Sep 17 00:00:00 2001 From: Brian Rue Date: Fri, 28 Jun 2013 15:55:23 -0700 Subject: Use consistent example for demonstrating integer vs. float division --- python.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python.html.markdown b/python.html.markdown index 20e4f3a8..93fe5f25 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -36,7 +36,7 @@ to Python 2.x. Look for another tour of Python 3 soon! # To fix division we need to learn about floats. 2.0 # This is a float -5.0 / 2.0 #=> 2.5 ahhh...much better +11.0 / 4.0 #=> 2.75 ahhh...much better # Enforce precedence with parentheses (1 + 3) * 2 #=> 8 -- cgit v1.2.3 From 301e021fb637a38ec6390c5f8bb445616abf902e Mon Sep 17 00:00:00 2001 From: Aditya Bhargava Date: Fri, 28 Jun 2013 17:01:58 -0700 Subject: adding haskell --- haskell.html.markdown | 290 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 290 insertions(+) create mode 100644 haskell.html.markdown diff --git a/haskell.html.markdown b/haskell.html.markdown new file mode 100644 index 00000000..5ffc63f3 --- /dev/null +++ b/haskell.html.markdown @@ -0,0 +1,290 @@ +--- +language: haskell +author: Adit Bhargava +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 +makes coding a real joy for me. + +```haskell +-- Single line comments start with two dashes. +{- Multiline comments can be enclosed +in a block like this. +-} + +---------------------------------------------------- +-- 1. Primitive Datatypes and Operators +---------------------------------------------------- + +-- You have numbers +3 -- 3 + +-- Math is what you would expect +1 + 1 -- 2 +8 - 1 -- 7 +10 * 2 -- 20 +35 / 5 -- 7.0 + +-- Division is not integer division by default +35 / 4 -- 8.75 + +-- integer division +35 `div` 4 -- 8 + +-- Boolean values are primitives +True +False + +-- Boolean operations +not True -- False +not False -- True +1 == 1 -- True +1 /= 1 -- False +1 < 10 -- True + +-- Strings and characters +"This is a string." +'a' -- character +'You cant use single quotes for strings.' -- error! + +-- Strings can be added too! +"Hello " ++ "world!" #=> "Hello world!" + +-- A string can be treated like a list of characters +"This is a string" !! 0 #=> 'T' + + +---------------------------------------------------- +-- Lists and Tuples +---------------------------------------------------- + +-- Every element in a list must have the same type. +-- Two lists that are the same +[1, 2, 3, 4, 5] +[1..5] + +-- You can also have infinite lists in Haskell! +[1..] -- a list of all the natural numbers + +-- 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 + +-- more list operations + +head [1..5] -- 1 +tail [1..5] -- [2, 3, 4, 5] +init [1..5] -- [1, 2, 3, 4] +last [1..5] -- 5 + +-- list comprehensions +[x*2 | x <- [1..5]] -- [2, 4, 6, 8, 10] + +-- with a conditional +[x*2 | x <- [1..5], x*2 > 4] # [6, 8, 10] + +-- Every element in a tuple can be a different type, but a tuple has a fixed length. +-- A tuple: +("haskell", 1) + +-- accessing elements of a tuple +fst ("haskell", 1) -- "haskell" +snd ("haskell", 1) -- 1 + +---------------------------------------------------- +-- 3. Functions +---------------------------------------------------- +-- A simple function that takes two variables +add a b = a + b + +-- Using the function +add 1 2 -- 3 + +-- You can also put the function name between the two arguments with backticks: +1 `add` 2 -- 3 + +-- You can also define functions that have no characters! This lets you define +-- your own operators: + +-- Here's an operator that does integer division +(//) a b = a `div` b +35 // 4 -- 8 + +-- Guards: an easy way to do branching in functions +fib x + | x < 2 = x + | otherwise = fib (x - 1) + fib (x - 2) + +-- You can do the same thing with pattern matching. +fib 1 = 1 +fib 2 = 2 +fib x = fib (x - 1) + fib (x - 2) + +-- So we have given three different definitions for fib. +-- Haskell will automatically call the first function that matches +-- the pattern of the value. + +-- 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: +map func [x] = [func x] +map func (x:xs) = func x:(map 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] + +-- 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. +foldl1 (\acc x -> acc + x) [1..5] -- 15 + +---------------------------------------------------- +-- 4. Some fancy things you can do with 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 +rest of the arguments. + +add a b = a + b +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 5 -- 15 + +-- function composition +the (.) function 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) + +-- (5 + 10) * 5 = 75 +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: + +-- before +(even (double 7)) -- true + +-- after +even . double $ 7 -- true + +---------------------------------------------------- +-- 5. Type signatures +---------------------------------------------------- + +Haskell has a very strong type system, and everything has a type signature. + +Some basic types: +5 :: Integer +"hello" :: String +True :: Bool + +Functions have types too. +Not takes a boolean and returns a boolean: +not :: Bool -> Bool + +Here's a function that takes two arguments: +add :: Integer -> Integer -> Integer + +---------------------------------------------------- +-- 6. Control Flow +---------------------------------------------------- + +-- if statements +haskell = if 1 == 1 then "awesome" else "awful" -- haskell = "awesome" + +-- if statements 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 in Haskell + +case args of + "help" -> printHelp + "start" -> startProgram + _ -> putStrLn "bad args" + + +-- loops: recursion +-- Haskell doesn't have loops because it uses recursion instead. + +-- map a function over every element in an array + +map (*2) [1..5] -- [2, 4, 6, 8, 10] + +-- you can make a for function using map +for array func = map func array + +-- and then use it + +for [0..5] $ \i -> print i + +---------------------------------------------------- +-- 7. Data Types +---------------------------------------------------- + +-- Here's how you make your own data type in Haskell + +data Color = Red | Blue | Green + +Now you can use it in a function: + +say :: Color -> IO String +say Red = putStrLn "You are Red!" +say Blue = putStrLn "You are Blue!" +say Green = putStrLn "You are Green!" + +-- Your data types can have parameters too: + +data Maybe a = Nothing | Just a + +-- These are all of type Maybe +Nothing +Just "hello" +Just 1 + +---------------------------------------------------- +-- 8. The Haskell REPL +---------------------------------------------------- + +-- Start the repl by typing `ghci`. +-- Now you can type in Haskell code. Any new values +-- need to be created with `let`: + +let foo = 5 + +-- You can see the type of any value with `:t`: + +>:t foo +foo :: Integer +``` + +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 [] = [] +qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater + where lesser = filter (< p) xs + greater = filter (>= p) xs +``` -- cgit v1.2.3 From be8f92d80454c8a25a262945366e962803a3b3a0 Mon Sep 17 00:00:00 2001 From: Aditya Bhargava Date: Fri, 28 Jun 2013 17:09:34 -0700 Subject: fixes --- haskell.html.markdown | 68 +++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 38 deletions(-) diff --git a/haskell.html.markdown b/haskell.html.markdown index 5ffc63f3..a5a6117f 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -50,10 +50,10 @@ not False -- True 'You cant use single quotes for strings.' -- error! -- Strings can be added too! -"Hello " ++ "world!" #=> "Hello world!" +"Hello " ++ "world!" -- "Hello world!" -- A string can be treated like a list of characters -"This is a string" !! 0 #=> 'T' +"This is a string" !! 0 -- 'T' ---------------------------------------------------- @@ -75,11 +75,9 @@ not False -- True 0:[1..5] -- [0, 1, 2, 3, 4, 5] -- indexing into a list - [0..] !! 5 -- 4 -- more list operations - head [1..5] -- 1 tail [1..5] -- [2, 3, 4, 5] init [1..5] -- [1, 2, 3, 4] @@ -89,7 +87,7 @@ last [1..5] -- 5 [x*2 | x <- [1..5]] -- [2, 4, 6, 8, 10] -- with a conditional -[x*2 | x <- [1..5], x*2 > 4] # [6, 8, 10] +[x*2 | x <- [1..5], x*2 > 4] -- [6, 8, 10] -- Every element in a tuple can be a different type, but a tuple has a fixed length. -- A tuple: @@ -112,9 +110,7 @@ add 1 2 -- 3 1 `add` 2 -- 3 -- You can also define functions that have no characters! This lets you define --- your own operators: - --- Here's an operator that does integer division +-- your own operators! Here's an operator that does integer division (//) a b = a `div` b 35 // 4 -- 8 @@ -123,20 +119,19 @@ fib x | x < 2 = x | otherwise = fib (x - 1) + fib (x - 2) --- You can do the same thing with pattern matching. +-- 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. fib 1 = 1 fib 2 = 2 fib x = fib (x - 1) + fib (x - 2) --- So we have given three different definitions for fib. --- Haskell will automatically call the first function that matches --- the pattern of the value. - -- 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: +-- 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) @@ -149,12 +144,12 @@ map (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7] foldl1 (\acc x -> acc + x) [1..5] -- 15 ---------------------------------------------------- --- 4. Some fancy things you can do with functions +-- 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 -rest of the arguments. +-- it gets "curried". That means it returns a function that takes the +-- rest of the arguments. add a b = a + b foo = add 10 -- foo is now a function that takes a number and adds 10 to it @@ -165,10 +160,9 @@ foo = (+10) foo 5 -- 15 -- function composition -the (.) function 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. - +-- the (.) function 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) -- (5 + 10) * 5 = 75 @@ -176,9 +170,9 @@ 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: +-- 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: -- before (even (double 7)) -- true @@ -190,18 +184,18 @@ even . double $ 7 -- true -- 5. Type signatures ---------------------------------------------------- -Haskell has a very strong type system, and everything has a type signature. +-- Haskell has a very strong type system, and everything has a type signature. -Some basic types: +-- Some basic types: 5 :: Integer "hello" :: String True :: Bool -Functions have types too. -Not takes a boolean and returns a boolean: +-- Functions have types too. +-- `not` takes a boolean and returns a boolean: not :: Bool -> Bool -Here's a function that takes two arguments: +-- Here's a function that takes two arguments: add :: Integer -> Integer -> Integer ---------------------------------------------------- @@ -216,19 +210,13 @@ haskell = if 1 == 1 then "awesome" else "awful" --- case statements - --- Here's how you could parse command line arguments in Haskell - +-- case statements: Here's how you could parse command line arguments in Haskell case args of "help" -> printHelp "start" -> startProgram _ -> putStrLn "bad args" - --- loops: recursion -- Haskell doesn't have loops because it uses recursion instead. - -- map a function over every element in an array map (*2) [1..5] -- [2, 4, 6, 8, 10] @@ -237,8 +225,10 @@ map (*2) [1..5] -- [2, 4, 6, 8, 10] for array func = map func array -- and then use it +for [0..5] $ \i -> show i -for [0..5] $ \i -> print i +-- we could've written that like this too: +for [0..5] show ---------------------------------------------------- -- 7. Data Types @@ -248,7 +238,7 @@ for [0..5] $ \i -> print i data Color = Red | Blue | Green -Now you can use it in a function: +-- Now you can use it in a function: say :: Color -> IO String say Red = putStrLn "You are Red!" @@ -288,3 +278,5 @@ qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater where lesser = filter (< p) xs greater = filter (>= p) xs ``` + +Haskell is easy to install. Get it [here](http://www.haskell.org/platform/). -- cgit v1.2.3 From 86885dab5ca005ba72c21b80f7f55a9b92e17a6d Mon Sep 17 00:00:00 2001 From: Kai Mallea Date: Fri, 28 Jun 2013 21:47:31 -0400 Subject: Update pointer examples and comments --- c.html.markdown | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/c.html.markdown b/c.html.markdown index 15bfa05e..3f9b6c61 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -194,26 +194,41 @@ printf("%d\n", (short) 65537); // => 1 (Max short = 65535) // Pointers /////////////////////////////////////// -// You can retrieve the memory address of your variables, -// then mess with them. +// A pointer is a variable declared to store a memory address. Its declaration will +// also tell you the type of data it points to. You can retrieve the memory address +// of your variables, then mess with them. int x = 0; printf("%p\n", &x); // Use & to retrieve the address of a variable // (%p formats a pointer) // => Prints some address in memory; +// Pointer types end with * in their declaration +int* px; // px is a pointer to an int +px = &x; // Stores the address of x in px +printf("%p\n", px); // => Prints some address in memory + +// To retreive the value at the address a pointer is pointing to, +// put * in front to de-reference it. +printf("%d\n", *px); // => Prints 0, the value of x, which is what px is pointing to the address of + +// You can also change the value the pointer is pointing to. +// We'll have to wrap the de-reference in parenthesis because +// ++ has a higher precedence than *. +(*px)++; // Increment the value px is pointing to by 1 +printf("%d\n", *px); // => Prints 1 +printf("%d\n", x); // => Prints 1 + int x_array[20]; // Arrays are a good way to allocate a contiguous block of memory int xx; for(xx=0; xx<20; xx++){ x_array[xx] = 20 - xx; } // Initialize x_array to 20, 19, 18,... 2, 1 -// Pointer types end with * int* x_ptr = x_array; -// This works because arrays are pointers to their first element. +// This works because an array name is bound to the address of its first element -// Put a * in front to de-reference a pointer and retrieve the value, -// of the same type as the pointer, that the pointer is pointing at. +// Arrays are pointers to their first element printf("%d\n", *(x_ptr)); // => Prints 20 printf("%d\n", x_array[0]); // => Prints 20 -- cgit v1.2.3 From db168d11be3306c88eb90f775fda15fcafe3ce82 Mon Sep 17 00:00:00 2001 From: Kai Mallea Date: Fri, 28 Jun 2013 21:54:28 -0400 Subject: Update comment --- c.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/c.html.markdown b/c.html.markdown index 3f9b6c61..36bd07fd 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -225,6 +225,7 @@ for(xx=0; xx<20; xx++){ x_array[xx] = 20 - xx; } // Initialize x_array to 20, 19, 18,... 2, 1 +// Declare a pointer of type int and initialize it to point to x_array int* x_ptr = x_array; // This works because an array name is bound to the address of its first element -- cgit v1.2.3 From 30e0d68cd9d3ad2fe35bb895feb1388539a91e5a Mon Sep 17 00:00:00 2001 From: Dario Sneidermanis Date: Fri, 28 Jun 2013 23:19:25 -0300 Subject: Fix precision typo with C integer types --- c.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/c.html.markdown b/c.html.markdown index 15bfa05e..413aad5f 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -50,8 +50,8 @@ short x_short = 0; char x_char = 0; char y_char = 'y'; // Char literals are quoted with '' -long x_long = 0; // Still 32 bytes for historical reasons -long long x_long_long = 0; // Guaranteed to be at least 64 bytes +long x_long = 0; // Still 32 bits for historical reasons +long long x_long_long = 0; // Guaranteed to be at least 64 bits // 32-bit floating-point decimal float x_float = 0.0; -- cgit v1.2.3