Personal tools

Learn Haskell in 10 minutes

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
 
Line 1: Line 1:
 
 
== Overview ==
 
== Overview ==
   
Line 10: Line 9:
 
You can type most math expressions directly into ghci and get an answer.
 
You can type most math expressions directly into ghci and get an answer.
   
Prelude> 3 * 5
+
Prelude> <hask>3 * 5</hask>
 
15
 
15
Prelude> 4^2 - 1
+
Prelude> <hask>4 ^ 2 - 1</hask>
 
15
 
15
Prelude> (1 - 5)^(3 * 2 - 4)
+
Prelude> <hask>(1 - 5)^(3 * 2 - 4)</hask>
 
16
 
16
   
 
Strings are in "double quotes." You can concatenate them with ++.
 
Strings are in "double quotes." You can concatenate them with ++.
   
Prelude> "Hello"
+
Prelude> <hask>"Hello"</hask>
 
"Hello"
 
"Hello"
Prelude> "Hello" ++ ", Haskell"
+
Prelude> <hask>"Hello" ++ ", Haskell"</hask>
 
"Hello, Haskell"
 
"Hello, Haskell"
   
 
Calling functions is done by putting the arguments directly after the function. There are no parentheses as part of the function call:
 
Calling functions is done by putting the arguments directly after the function. There are no parentheses as part of the function call:
   
Prelude> succ 5
+
Prelude> <hask>succ 5</hask>
 
6
 
6
Prelude> truncate 6.59
+
Prelude> <hask>truncate 6.59</hask>
 
6
 
6
Prelude> round 6.59
+
Prelude> <hask>round 6.59</hask>
 
7
 
7
Prelude> sqrt 2
+
Prelude> <hask>sqrt 2</hask>
 
1.4142135623730951
 
1.4142135623730951
Prelude> not (5 < 3)
+
Prelude> <hask>not (5 < 3)</hask>
 
True
 
True
Prelude> gcd 21 14
+
Prelude> <hask>gcd 21 14</hask>
 
7
 
7
   
Line 43: Line 42:
 
I/O actions can be used to read from and write to the console. Some common ones include:
 
I/O actions can be used to read from and write to the console. Some common ones include:
   
Prelude> putStrLn "Hello, Haskell"
+
Prelude> <hask>putStrLn "Hello, Haskell"</hask>
 
Hello, Haskell
 
Hello, Haskell
Prelude> putStr "No newline"
+
Prelude> <hask>putStr "No newline"</hask>
No newlinePrelude> print (5 + 4)
+
No newlinePrelude> <hask>print (5 + 4)</hask>
 
9
 
9
Prelude> print (1 < 2)
+
Prelude> <hask>print (1 < 2)</hask>
 
True
 
True
   
Line 55: Line 54:
 
If you need multiple I/O actions in one expression, you can use a do block. Actions are separated by semicolons.
 
If you need multiple I/O actions in one expression, you can use a do block. Actions are separated by semicolons.
   
Prelude> do { putStr "2 + 2 = " ; print (2 + 2) }
+
Prelude> <hask>do { putStr "2 + 2 = " ; print (2 + 2) }</hask>
 
2 + 2 = 4
 
2 + 2 = 4
Prelude> do { putStrLn "ABCDE" ; putStrLn "12345" }
+
Prelude> <hask>do { putStrLn "ABCDE" ; putStrLn "12345" }</hask>
 
ABCDE
 
ABCDE
 
12345
 
12345
Line 63: Line 62:
 
Reading can be done with getLine (which gives back a String) or readLn (which gives back whatever type of value you want). The <- symbol is used to assign a value to the result of an I/O action.
 
Reading can be done with getLine (which gives back a String) or readLn (which gives back whatever type of value you want). The <- symbol is used to assign a value to the result of an I/O action.
   
Prelude> do { n <- readLn ; print (n^2) }
+
Prelude> <hask>do { n <- readLn ; print (n^2) }</hask>
 
4
 
4
 
16
 
16

Revision as of 05:45, 13 July 2007

Contents

1 Overview

Haskell is a functional (that is, everything is done with function calls), statically, implicitly typed (types are checked by the compiler, but you don't have to declare them), lazy (nothing is done until it needs to be) language. It's closest popular relative is probably the ML family of languages.

The most common Haskell compiler is GHC. You can download GHC from http://www.haskell.org/ghc/download_ghc_661.html. GHC binaries are available for Linux, FreeBSD, MacOS, Windows, and Solaris. Once you've installed GHC, you get two programs you're interested in right now: ghc, and ghci. The first compiles Haskell libraries or applications to binary code. The second is an interpreter that lets you write Haskell code and get feedback right away.

2 Simple Expressions

You can type most math expressions directly into ghci and get an answer.

Prelude>
3 * 5
15
Prelude>
4 ^ 2 - 1
15
Prelude>
(1 - 5)^(3 * 2 - 4)
16

Strings are in "double quotes." You can concatenate them with ++.

Prelude>
"Hello"
"Hello"
Prelude>
"Hello" ++ ", Haskell"
"Hello, Haskell"

Calling functions is done by putting the arguments directly after the function. There are no parentheses as part of the function call:

Prelude>
succ 5
6
Prelude>
truncate 6.59
6
Prelude>
round 6.59
7
Prelude>
sqrt 2
1.4142135623730951
Prelude>
not (5 < 3)
True
Prelude>
gcd 21 14
7

3 The Console

I/O actions can be used to read from and write to the console. Some common ones include:

Prelude>
putStrLn "Hello, Haskell"
Hello, Haskell
Prelude>
putStr "No newline"
No newlinePrelude>
print (5 + 4)
9
Prelude>
print (1 < 2)
True

The putStr and putStrLn functions output strings. The print function outputs any type of value. (If you print a string, it will have quotes around it.)

If you need multiple I/O actions in one expression, you can use a do block. Actions are separated by semicolons.

Prelude>
do { putStr "2 + 2 = " ; print (2 + 2) }
2 + 2 = 4
Prelude>
do { putStrLn "ABCDE" ; putStrLn "12345" }
ABCDE
12345

Reading can be done with getLine (which gives back a String) or readLn (which gives back whatever type of value you want). The <- symbol is used to assign a value to the result of an I/O action.

Prelude>
do { n <- readLn ; print (n^2) }
4
16

(The 4 was input. The 16 was a result.)

There is actually another way to write do blocks. If you leave off the braces and semicolons, then indentation becomes significant. This doesn't work so well in ghci, but try putting the file in a source file (say, Test.hs) and build it.

main = do putStrLn "What is 2 + 2?"
          x <- readLn
          if x == 4
              then putStrLn "You're right!"
              else putStrLn "You're wrong!"

You can build with ghc --make Test.hs, and the result will be called Test. (On Windows, Test.exe) You get an if statement as a bonus.

Every line that starts in the same column as the first putStrLn is part of the do block. This is called "layout", and Haskell uses it to avoid making you put in statement terminators and braces all the time. (The then and else phrases are indented

4 Simple Types

So far, not a single type has been mentioned