Installation
Under centos/Fedora
# Yum install ghc
Version
[[Email protected] Haskell] # ghc-V
Glasgow Haskell compiler, version 7.0.4, for Haskell 98, Stage 2 booted by ghc version 7.0.4
Change Prompt
Echo: set prompt "ghci>"> ~ /. Ghci
Command
# Boolean
Ghci> true & false
False
# Call function, get successor of 8
Ghci> succ 8
9
# Load scripts
# Echo doubleme x = x + x> baby. Hs
Ghci>: l baby. Hs
[1 of 1] compiling main (baby. Hs, interpreted)
OK, modules loaded: Main.
Ghci> doubleme 9
18
# Concatenation between list and string
Ghci> [1, 2, 4] ++ [9, 10, 11, 12]
[1, 2, 3, 4, 9, 10, 11, 12]
Or
Ghci> 'A': "Small Cat"
"A small cat"
Or
Ghci> 5: [1, 2]
[5, 1, 2]
# List Operation
# Get the element that's Index = 3
Ghci> "123456 "!! 3
'4'
# Comparing
Ghci> [3, 2, 1]> [2, 1, 0]
True
# Head vs tail, init vs 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]
# Length
Ghci> length [5, 4, 3, 2, 1]
5
# Is null or not
Ghci> null [1, 2, 3]
False
Ghci> null []
True
# Reverse list
Ghci> reverse [5, 4, 3, 2, 1]
[1, 2, 3, 4, 5]
# Take the first n elements
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]
[]
# Drop the first n elements
Ghci> drop 3 [,]
[1, 5, 6]
Ghci> drop 0 [1, 2, 4]
[1, 2, 3, 4]
Ghci> drop 100 [,]
[]
# Element in list or not
Ghci> 4 'elem '[1, 4]
True
Or
Ghci> ELEM 4 [1, 4]
True
# Range
Ghci> [1 .. 4]
[1, 2, 3, 4]
# Replicate
Ghci> replicate 3 10
[10, 10, 10]
# List Comprehension
Ghci> [x * 2 | x <-[1 .. 10]
[,]
Ghci> [x * 2 | x <-[1 .. 10], x * 2> = 12]
[, 20]
Note
Doublesmallnumber 'is valid as a method name
[Learning you a Haskell for great goods!] Chapter 01 starting out