Lisperati Haskell tutorial.pdf
(
1637 KB
)
Pobierz
355480645 UNPDF
by Conrad Barski, M.D.
Mail
Note To Advanced Haskellers
Hi everyone,
welcome to my Haskell tutorial!
There's other tutorials out there, but you'll like this one the
best
for sure
: You can just cut and paste the code from this
tutorial bit by bit, and in the process, your new program will
create magically create more and more cool graphics along
the way... The final program will have less than 100 lines of
Haskell
[1]
and will organize a mass picnic in an arbitrarily-
shaped public park map and will print pretty pictures
showing where everyone should sit! (
Here's
what the final
product will look like, if you're curious...)
The code in this tutorial is a simplified version of the code
I'm using to organize flash mob picnics for my art project,
picnicmob.org
.
.. Be sure to check out the site and sign up if
you live in one of the cities we're starting off with :-)
NEXT
[1] -
Lines of active code only, not counting optional function
signatures. Intermediate debug output lines excluded. May
cause eye irritation if viewed for prolonged periods of time.
Speak to your physician to find out if this tutorial is the best
treatment option for you.
How To Organize a Picnic on a Computer
Ok, so here's what the program is going to do... On the
picnicmob.org website, people answer a bunch of random
questions. What we want our program to do is take a picture of
a city park, a list of answers people gave on the questions, and
have it figure out where people should sit in the park so that
their neighbors are as similar as possible to them, based on
similar answers for the questions. We're going to do this using
a standard simulated annealing algorithm. (Read
this
for more
info... we'll be describing the basics below, too...)
Installing GHC Haskell On Your Computer
The only preparation you need to do for this tutorial is install
the Glasgow Haskell Compiler- You can get the latest version
from
here
.
It's a very well supported and very fast compiler.
We're just going to use the basic GHC libraries in this tutorial-
No other installs are needed.
Hello World! Let's Have a Picnic!
Now you have all you need to run the "Hello World" program
below- Just copy the code into a file called
tutorial.hs
:
import Data.List
import Text.Regex
import System.Random
import Data.Ord
type Point = (Float,Float)
type Color = (Int,Int,Int)
type Polygon = [Point]
type Person = [Int]
type Link = [Point]
type Placement = [(Point,Person)]
type EnergyFunction a = a -> Int
type TemperatureFunction = Int -> Int -> Float
type TransitionProbabilityFunction = Int -> Int -> Float -> Float
type MotionFunction a = StdGen -> a -> (StdGen,a)
main = do
putStr "Hello World! Let's have a picnic! \n"
After you've copied the code to a file, just run your new
program from the command line as shown below:
> runHaskell tutorial.hs
Hello World! Let's have a picnic!
*phew* That was easy!
How Does This Code Work?
As you can see, there's lots of stuff in this "Hello World" that
doesn't seem necessary... Actually, only the last two lines
(starting at
main = do
) are really needed to print our
message... The rest is just header stuff that we'll use later in
the tutorial...
At the top, you can see some things we're
importing
out of
the standard GHC libraries:
1. The
Data.List
module has extra functions for
slicing and dicing lists- Lists are fundamental in
Haskell programming and we'll be using them a lot in
this program.
2. The
Text.Regex
module let's us do funky things
with text strings using
regular expressions
- If you
don't know what this means, read
this tutorial first
.
Every programmer should know about regular
expressions.
3. The
System.Random
module, as you've probably
guessed, lets us use random numbers, essential for
simulated annealing.
4. The
Data.Ord
just contains a single function I'm
really fond of, called
comparing
. I'm not happy if I
can't use
comparing
, and I think we all want this to
be a
happy
tutorial...
The next few lines define different types that we're going to
use in our picnic program... Haskell programmers are really
fond of lots of types, because Haskell compilers are basically
just these crazy type-crunching machines that will take any
types you give them and if they find anything questionable
about how you're using types they will let you know early,
preventing your program from becoming buggy. The way
Haskell compilers handle types puts them head and shoulders
above most other compilers (Read more about Hindley-
Milner type inference
here.
)
The first few
type
lines should be pretty self-explanatory,
for a program that's going to work with pictures: A
Point
is
just a pair of floating numbers, a
Color
is an rgb triple, a
Polygon
is just a list of
Point
s, a
Person
is just a list of
the answers they gave in the questionaire- Very simple stuff...
The lower
type
lines are a bit more difficult to understand-
They describe the
types of the four functions
that are used in
all simulated annealing algorithms... we'll be describing those
later on. For now, just know that in Haskell you want to look
at the last arrow, which looks like this
>
... Anything to the
left of that arrow is a parameter into our function- The thing
to the right is the return value of the function. For instance,
the
EnergyFunction
is a function that takes an arbitrary
type
a
(which will be a seating arrangement of picnickers)
and returns an integer, which is the "energy" of our picnic. A
low energy will mean that all the picnickers are happy with
where they're sitting. We'll explain these funcitons more later.
What's Good about this Code?
What's good is that in Haskell we can create all kinds of
types to describe what we want to do and the Haskell
compiler is very good at catching potential errors for us
ahead of time. Defining types in Haskell is usually a
choice
and not a
necessity
- This program would run just fine, even if
all the type declarations were missing... The Haskell compiler
would just figure them out on its own!
Plik z chomika:
zuchel
Inne pliki z tego folderu:
haskell2010.pdf
(1465 KB)
LISt Proccessing in HASKELL.pdf
(108 KB)
Lisperati Haskell tutorial.pdf
(1637 KB)
Yet Another Haskell Tutorial (YANT).pdf
(843 KB)
SYMBOLON-HCAS-slides.pdf
(237 KB)
Inne foldery tego chomika:
(lisp)
CEO
CLIPS
DSL
elixir
Zgłoś jeśli
naruszono regulamin