A brief introduction to Haskell
From HaskellWiki
DonStewart (Talk | contribs) (wibbles) |
DonStewart (Talk | contribs) (more tweaks) |
||
Line 9: | Line 9: | ||
* A language [http://haskell.org/haskellwiki/History_of_Haskell developed] by the programming languages [http://haskell.org/haskellwiki/Research_papers research] community. |
* A language [http://haskell.org/haskellwiki/History_of_Haskell developed] by the programming languages [http://haskell.org/haskellwiki/Research_papers research] community. |
||
− | * Is a lazy, purely functional language (that also has imperative features such as side effects and mutable state, as well strict evaluation) |
+ | * Is a lazy, purely functional language (that also has imperative features such as side effects and mutable state, along with strict evaluation) |
+ | * Born as an open source vehicle for programming language research |
||
* One of the youngest children of ML and Lisp |
* One of the youngest children of ML and Lisp |
||
* Particularly useful for manipulating data structures, (i.e. [http://haskell.org/ghc compilers] and [http://pugscode.org interpreters]), and parallel programming |
* Particularly useful for manipulating data structures, (i.e. [http://haskell.org/ghc compilers] and [http://pugscode.org interpreters]), and parallel programming |
||
Line 20: | Line 20: | ||
* ''1997''. Haskell 1.4 |
* ''1997''. Haskell 1.4 |
||
* ''1998''. Haskell 98 |
* ''1998''. Haskell 98 |
||
− | * ''~2007''. Haskell Prime |
+ | * ''2000-2006''. Period of rapid language and community growth |
+ | * ''~2007''. Haskell Prime |
||
− | == Haskell Novelties == |
+ | [http://haskell.org/haskellwiki/Implementations Implementations]: |
+ | * [http://haskell.org/ghc GHC] |
||
+ | * [http://haskell.org/hugs Hugs] |
||
+ | * [http://haskell.org/haskellwiki/Yhc YHC] |
||
+ | * [http://repetae.net/computer/jhc/JHC] |
||
+ | * [http://haskell.org/nhc98 nhc98] |
||
+ | * [http://www.cs.uu.nl/helium/ Helium] |
||
+ | * [http://www.cs.chalmers.se/~augustss/hbc/hbc.html HBC] |
||
+ | |||
+ | == Haskell features == |
||
Has some novel features relative to Java (and C++). |
Has some novel features relative to Java (and C++). |
||
Line 35: | Line 35: | ||
* Pattern matching on data structures -- data structures are first class! |
* Pattern matching on data structures -- data structures are first class! |
||
* Parametric polymorphism |
* Parametric polymorphism |
||
− | * Bounded polymorphism (based on type classes) |
+ | * Bounded parametric polymorphism |
− | These are all conceptually more advanced ideas, for the average imperative language. |
+ | These are all conceptually more [http://haskell.org/haskellwiki/Research_papers/Type_systems advanced ideas]. |
− | Compared to similar functional languages, [http://haskell.org/ghc GHC] |
+ | Compared to similar functional languages, Haskell differs in its support for: |
− | Haskell contains several newer language features: |
||
− | * Monads |
+ | * Lazy evaluation |
+ | * Pure by default |
||
+ | * Monadic effects |
||
* Type classes |
* Type classes |
||
+ | * Syntax based on layout |
||
+ | |||
+ | The GHC Haskell compiler, in particular, provides some interesting |
||
+ | extensions: |
||
+ | |||
* Generalised algebraic data types |
* Generalised algebraic data types |
||
− | * Impredicative type system |
+ | * Impredicative types system |
* Software transactional memory |
* Software transactional memory |
||
* Parallel, SMP runtime system |
* Parallel, SMP runtime system |
||
Line 52: | Line 57: | ||
Read the [http://haskell.org/onlinereport/ language definition] section |
Read the [http://haskell.org/onlinereport/ language definition] section |
||
of the manual to supplement these notes. For more depth and examples |
of the manual to supplement these notes. For more depth and examples |
||
− | [http://haskell.org/haskellwiki/Books_and_tutorials see here[http://haskell.org/haskellwiki/Books_and_tutorials]. |
+ | [http://haskell.org/haskellwiki/Books_and_tutorials see here]. |
=== Interacting with the language === |
=== Interacting with the language === |
||
− | Interacting with Haskell via the GHCi interpreter: |
+ | [http://haskell.org/haskellwiki/Haskell_in_5_steps Interacting with Haskell] via the GHCi interpreter: |
* expressions are entered at the prompt |
* expressions are entered at the prompt |
Revision as of 01:00, 27 October 2006
Inspired by the Introduction to OCaml.
Contents |
1 Background
Haskell is:
- A language developed by the programming languages research community.
- Is a lazy, purely functional language (that also has imperative features such as side effects and mutable state, along with strict evaluation)
- Born as an open source vehicle for programming language research
- One of the youngest children of ML and Lisp
- Particularly useful for manipulating data structures, (i.e. compilers and interpreters), and parallel programming
- 1990. Haskell 1.0
- 1991. Haskell 1.1
- 1993. Haskell 1.2
- 1996. Haskell 1.3
- 1997. Haskell 1.4
- 1998. Haskell 98
- 2000-2006. Period of rapid language and community growth
- ~2007. Haskell Prime
2 Haskell features
Has some novel features relative to Java (and C++).
- Immutable variables by default (mutable state programmed via monads)
- Pure by default (side effects are programmed via monads)
- Lazy evaluation: results are only computed if they're required (strictness optional)
- Everything is an expression
- Completely higher-order functions: functions can be defined anywhere in the code, passed as arguments, and returned as values.
- Both compiled and interpreted implementations available
- Full type inference -- type declarations optional
- Pattern matching on data structures -- data structures are first class!
- Parametric polymorphism
- Bounded parametric polymorphism
These are all conceptually more advanced ideas.
Compared to similar functional languages, Haskell differs in its support for:
- Lazy evaluation
- Pure by default
- Monadic effects
- Type classes
- Syntax based on layout
The GHC Haskell compiler, in particular, provides some interesting extensions:
- Generalised algebraic data types
- Impredicative types system
- Software transactional memory
- Parallel, SMP runtime system
3 The Basics
Read the language definition section of the manual to supplement these notes. For more depth and examples see here.
3.1 Interacting with the language
Interacting with Haskell via the GHCi interpreter:
- expressions are entered at the prompt
- newline signals end of input
Here is a GHCi sessoin, starting from a UNIX prompt.
$ ghci ___ ___ _ / _ \ /\ /\/ __(_) / /_\// /_/ / / | | GHC Interactive, version 6.4.2, for Haskell 98. / /_\\/ __ / /___| | http://www.haskell.org/ghc/ \____/\/ /_/\____/|_| Type :? for help.
Loading package base-1.0 ... linking ... done.
Prelude> let x = 3 + 4
- Here the incredibly simple Haskell program islet x = 3+4
Prelude> :t x x :: Integer
We can ask the system what type it automaticaly inferred for our
variable.integer values.
Prelude> x 7
A variable evaluates to its value.
Prelude> x + 4 11
expressions.
Prelude> let x = 4 in x + 3 7
new binding for a variable with local scope.
Alternatively, declarations typed in at the top level are like an open-ended let:
Prelude> let x = 4 Prelude> let y = x + 3 Prelude> x * x 16 Prelude> :t x x :: Integer Prelude> :t y y :: Integer Prelude> :t x * x x * x :: Integer
Notice that type inference infers the correct type for all the expressions, without us having to ever specify the type explicitly.
4 Basic types
There is a range of basic types, defined in the language Prelude
Int -- bounded, word-sized integers Integer -- unbounded integers Double -- floating point values Char -- characters String -- strings () -- the unit type Bool -- booleans [a] -- lists (a,b) -- tuples / product types Either a b -- sum types Maybe a -- optional values
For example:
7 12312412412412321 3.1415 'x' "haskell" () True, False [1,2,3,4,5] ('x', 42) Left True, Right "string" Nothing, Just True
These types have all the usual operations on them, in the standard libraries.