hacklab.to

Haskell Workshop

by on Aug.06, 2011, under Uncategorized

Haskell is an awesome purely functional language that I’ve been becoming more and more obsessed with in recent months. I consider it to be the most elegant language I’ve ever worked in and want to introduce more people to it!

So, on Monday, August the 8th, 7pm/19:00 I’ll be teaching an introduction to Haskell workshop! Come and learn about laziness, type classes and (the awesome power of) monads!

Please note: This is not an introduction to programming class. You will not be able to follow along if you don’t have experience programming. I am planning to do a day long weekend Introduction to

Programming/Python workshop sometime in the near future that would be much more appropriate for you.

(Sorry about the late notice. I posted to discuss and then forgot to put up a blog post.)

Update: And it went awesome! There was a much bigger turn out than I expected. Here are some notes:

This is the first file I made as we went along. Unfortunately, we don’t seem to have syntax highlighting…

import Text.Parsec -- we'll use this later

-- Things that would be good to install
-- ghc
-- cabal

-- also: tryhaskell.org

-- operators are what you would expect, except:
-- doesn't equal is /=
-- concatonate is ++

-- Some operators you may not have seen before
-- $, applies right to left (ie, does nothing) but 
--     changes order of operation to get rid of brackets
-- ., compose right function with left

-- Operators are infix, other funcs prefix.
-- eg 1/2 but div 1 2
-- switch back and forth: (/) 1 2 and 1 `div` 2

-- Calculate the fibonacci sequence
fib :: Int -> Int
fib 0 = 1
fib 1 = 1
fib n = fib (n-1) + fib (n-2)

--Using a case
fib2 :: Int -> Int
fib2 n = case n of 
	0 -> 1
	1 -> 1
	n -> fib2 (n-1) + fib2 (n-2)

-- calculate points in a Julia set (constant k)
-- notice that Float -> (Float -> Int) is the same 
-- as Float -> FLoat -> Int by partial application
julia :: Float -> Float -> Int
julia k x = length $ take 100 $ takeWhile (\z -> abs(z)<2)$
	iterate (\z -> z^2 + k) x

-- Algebraic data types!
data Person = Eric | Jesus| Brian| Jane | John | Mary | Eve 

--To be able to output, we need to make Person an instance of Show
instance Show Person where
	show Eric = "Eric"
	show Jesus = "Jesus"
	show Brian = "Brian"
	show Jane = "Jane"
	show John = "John"
	show Mary = "Mary"
	show Eve  = "Eve"

-- gives you mother of a Person
mother :: Person -> Maybe Person

mother Eric  = Just Mary
mother Jesus = Just Mary
mother Brian = Just Mary
mother John  = Just Mary
mother Mary  = Just Jane
mother Jane = Just Eve
mother Eve = Nothing

-- Why didn't we have to make a show instance for Maybe Person?
-- It already exists, as a general definition, something like:
--data Maybe a = Nothing | Just a
--instance (Show a) => Show Maybe a where
--	show (Just a) = "Just" ++ show a
--	show Nothing = "Nothing"

-- Recursive Algebraic Data Types are possible. 
-- List is something like:
-- data [a] = [] | a : [a]
-- data List a = Empty | Item a (List a)


-- What makes a monad a monad? >>= (bind operator)
-- eg. [1,2,3] >>= (\a -> [a^2])
-- eg. Nothing >>= (\a -> Just $ a^2 )

-- We'd like to implement Cartesian Product
-- eg. [1,2] [3,4] -> [(1,3),(1,4), (2,3), (2,4)]
-- [1,2,3] >>= (\a -> ([4,5,6] >>= (\b -> [(a,b)])))

-- Since that is tedious, haskell has do notation
prod as bs = do 
	a <- as
	b <- bs
	return (a,b) -- return just monadifies things


greatgrandmother a = case (mother a) of
	Nothing -> Nothing
	Just b -> case (mother b) of
		Nothing -> Nothing
		Just c -> mother c

greatgrandmother2 a = do 
	b <- mother a
	c <- mother b
	d <- mother c
	return d

-- see hellowrold.hs

-- Parsec, the best parsing library ever!

element = string "H" <|> string "He"

elmentPair = do 
	elem <- element
	num <- digit
	return (elem, num)


-- do {a <- b; return a} = b >>= (\a -> return a)

And the second file:

-- Haskell handels IO by having main be an object that represents IO
-- It's a monad to make it easy to chain IO actions together :)
main = do 
	putStrLn "Hello World!"
	putStrLn "bah sheep!!"
	putStrLn "spit! Camel case! Grunt"
	a <- getLine
	putStrLn a

Some great resources are Learn You a Haskell For Great Good, Real World Haskell, and the Haskell Wikibook. A number of my examples were inspired, by things from these.

We also talked about the library Parsec a little bit, and looked at some of ucalc's source code for examples.


1 Trackback or Pingback for this entry

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!