Skip to main content

Haskell 基础入门

Features

  • statically typed
  • purely functional
  • type inference
  • concurrent
  • lazy
  • packages

Installer

curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh

Arithmetic

ghci> 2 + 15
17
ghci> 49 * 100
4900
ghci> 1892 - 1472
420
ghci> 5 / 2
2.5
ghci> (50 * 100) - 4999
1
ghci> 50 * 100 - 4999
1
ghci> 50 * (100 - 4999)
-244950

Boolean

ghci> True && False
False
ghci> True && True
True
ghci> False || True
True
ghci> not False
True
ghci> not (True && True)
False

Equality

ghci> 5 == 5
True
ghci> 1 == 0
False
ghci> 5 /= 5
False
ghci> 5 /= 4
True
ghci> "hello" == "hello"
True

Functions

ghci> succ 8
9
ghci> min 9 10
9
ghci> max 100 101
101
ghci> succ 9 + max 5 4 + 1
16
ghci> (succ 9) + (max 5 4) + 1
16
ghci> div 92 10
9

define a function

doubel x = x + x
doubleUs x y = x*2 + y*2
doubleSmallNumber x = if x > 100
then x
else x*2

load the function

ghci> :l double.hs
[1 of 1] Compiling Main ( double.hs, interpreted )
Ok, one module loaded.
ghci> double 4
8
ghci> doubleUs 4 5
18
ghci> doubleSmallNumber 100
200

When a function doesn't take any parameters, we usually say it's a definition (or a name).

Lists

ghci> let lostNumbers = [4,8,15,16,23,42]
ghci> lostNumbers
[4,8,15,16,23,42]

/// concatenate lists

ghci> [1,2,3,4] ++ [9,10,11,12]
[1,2,3,4,9,10,11,12]


/// string is a list of characters

ghci> "hello" ++ " " ++ "world"
"hello world"
ghci> ['w','o'] ++ ['o','t']
"woot"


/// put element in front of a list

ghci> 'A':" SMALL CAT"
"A SMALL CAT"
ghci> 5:[1,2,3,4,5]
[5,1,2,3,4,5]


/// index start from 0

ghci> "Steve Buscemi" !! 6
'B'
ghci> [9.4,33.2,96.2,11.2,23.25] !! 1
33.2

/// nested list

ghci> b = [[1,2,3,4],[5,3,3,3],[1,2,2,3,4],[1,2,3]]
ghci> b
[[1,2,3,4],[5,3,3,3],[1,2,2,3,4],[1,2,3]]
ghci> b ++ [[1,1,1,1]]
[[1,2,3,4],[5,3,3,3],[1,2,2,3,4],[1,2,3],[1,1,1,1]]
ghci> [6,6,6]:b
[[6,6,6],[1,2,3,4],[5,3,3,3],[1,2,2,3,4],[1,2,3]]
ghci> b !! 2
[1,2,2,3,4]

/// compare lists

ghci> [3,2,1] > [2,1,0]
True
ghci> [3,2,1] > [2,10,100]
True
ghci> [3,4,2] > [3,4]
True
ghci> [3,4,2] > [2,4]
True
ghci> [3,4,2] == [3,4,2]
True

/// head - first element
/// tail - all elements except the first
/// last - last element
/// init - all elements except the last

ghci> head [5,4,3,2,1]
5
ghci> tail [5,4,3,2,1]
[4,3,2,1]
ghci> last [5,4,3,2,1]
1
ghci> init [5,4,3,2,1]
[5,4,3,2]

list_op

/// length - get the length of a list
/// null - check if a list is empty
/// reverse - reverse a list
/// take - take n elements from the beginning of a list
/// drop - drop n elements from the beginning of a list
/// maximum - get the largest element of a list
/// minimum - get the smallest element of a list
/// sum - get the sum of a list
/// product - get the product of a list
/// elem - check if an element is in a list


ghci> length [5,4,3,2,1]
5
ghci> null [1,2,3]
False
ghci> null []
True
ghci> reverse [5,4,3,2,1]
[1,2,3,4,5]
ghci> take 3 [5,4,3,2,1]
[5,4,3]
ghci> take 1 [3,9,3]
[3]
ghci> take 5 [1,2]
[1,2]
ghci> take 0 [6,6,6]
[ ]
ghci> drop 3 [8,4,2,1,5,6]
[1,5,6]
ghci> drop 0 [1,2,3,4]
[1,2,3,4]
ghci> drop 100 [1,2,3,4]
[ ]
ghci> maximum [1,9,2,3,4]
9
ghci> minimum [8,4,2,1,5,6]
1
ghci> sum [5,2,1,6,3,2,5,7]
31
ghci> product [6,2,1,2]
24
ghci> product [1,2,5,6,7,9,2,0]
0
ghci> 4 `elem` [3,4,5,6]
True
ghci> 10 `elem` [3,4,5,6]

Ranges

ghci> [1..20]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
ghci> ['a'..'z']
"abcdefghijklmnopqrstuvwxyz"
ghci> ['K'..'Z']
"KLMNOPQRSTUVWXYZ"
ghci> [2,4..20]
[2,4,6,8,10,12,14,16,18,20]
ghci> [3,6..20]
[3,6,9,12,15,18]