-- Haskell Tutorial by Jordan Scales -- Assuming you've already done http://tryhaskell.org -- Use this however you wish, just give me credit :) -- scalesjordan@gmail.com upto n = [ 1 .. n ] twos n = [ 2 , 4 .. n ] -- pattern matching, haskell's smart, eh? threes n = [ 3 , 6 .. n ] iter n = [ x | x <- [ 1 .. n ]] -- set notation, see output. it's like a for loop. -- each part of 1 to n is stored in x, which is then appended to a list evens n = [ x | x <- [ 2 .. n ], x ` mod ` 2 == 0 ] -- the part after the comma is a condition -- only add x if its even f x = 8 * x + 5 func n = [ f x | x <- [ 1 .. n ]] -- set notation, except where adding f(x) to a list tri n = sum [ 1 .. n ] tetra n = sum [ x * x | x <- [ 1 .. n ]] fib 0 = 1 fib 1 = 1 fib n = fib ( n - 1 ) + fib ( n - 2 ) -- recursion is easy, so are base cases factors n = [ i | i <- [ 1 .. n ], n ` mod ` i == 0 ] isPrime n = ( sum . factors ) n == n + 1 -- compound statements (a . b) x is the same as a(b(x)) plusOne :: ( Integral a ) => a -> a -- type declaration, not necessary but good practice -- plusOne takes an integer "a", and also outputs an integer -- "plusOne :: a -> a" also works -- so does "plusOne:: Integer -> Integer" plusOne v = v + 1 -- this next one is cool whatisit :: Integer -> [ Char ] -- [Char] means an array of characters, AKA a string whatisit n | n == 2 = "Two!" | n == 3 = "Three!" | otherwise = "I don't know!" -- that's just a cool way to do basecases -- first part is a condition, followed by an equal sign, followed by code -- done defining, let's do shit! -- oh, btw this is a comment main = do -- 4 spaces = 1 tab -- printed a bunch of different ways to show formatting print ( upto 10 ) print ( twos 18 ) ( print . threes ) 46 -- compound statements (a . b) x is the same as a(b(x)) print ( iter 17 ) print ( evens 25 ) print ( func 12 ) print ( tri 10 ) print ( tetra 10 ) ( print . fib ) 10 print ( factors 1024 ) print ( isPrime 997 ) print ( plusOne 1771 ) print [( whatisit n ) | n <- [ 1 .. 5 ]]