some simple arithmetic


In [1]:
2 + 15


17

In [2]:
49 * 100


4900

In [3]:
1892 - 1472


420

In [4]:
5 /2


2.5

In [5]:
(50 * 100) - 499


4501

In [6]:
50 * 100 - 599


4401

In [7]:
50 * ( 100 - 499)


-19950

In [8]:
-- 5 * -3 will raise
5 * (-3)


-15

some boolean


In [9]:
True && False


Evaluate
Found:
True && False
Why Not:
False
False

In [10]:
True && True


Evaluate
Found:
True && True
Why Not:
True
True

In [11]:
False || True


Evaluate
Found:
False || True
Why Not:
True
True

In [12]:
not False


Evaluate
Found:
not False
Why Not:
True
True

In [13]:
not (True && True)


Evaluate
Found:
True && True
Why Not:
True
False

In [14]:
5 == 5


True

In [15]:
1 == 0


False

In [16]:
5 /= 5 -- not equal


False

In [17]:
5 /= 4


True

In [18]:
"hello" == "hello"


True

In [19]:
-- strong type language
-- this would raise: 5 + "hello" or 5 == True

some functions


In [20]:
-- 函数通常使用前缀调用方式.
succ 8


9

In [21]:
min 9 10


9

In [22]:
min 3.4 3.2


3.2

In [23]:
max 100 101


101

In [24]:
-- 函数调用有着最高的优先级
succ 9 + max 5 4 + 1
(succ 9) + (max 5 4) + 1


Redundant bracket
Found:
(succ 9) + (max 5 4)
Why Not:
succ 9 + (max 5 4)
Redundant bracket
Found:
(succ 9) + (max 5 4)
Why Not:
(succ 9) + max 5 4
16
16

In [25]:
--the following was not equal
succ 9 * 10
succ (9 * 10)


100
91

In [26]:
-- 如果函数接受两个参数,可以中坠调用方式
div 92 10
92 `div` 10


9
9

define function


In [27]:
doubleMe x = x + x

In [28]:
-- save the follow to `baby.hs` file.
-- in the ghci, use `:l baby`. 
-- then we can use double function
-- doubleMe 9
-- doubleMe 8.3

In [29]:
doubleUs x y = x*2 + y*2

In [30]:
doubleUs x y = doubleMe x + doubleMe y

In [31]:
-- function with if/else.
-- haskell中if后的else为必须。
doubleSmallNumber x = if x > 100
    then x
    else x * 2

In [32]:
-- if是一个表达式,使用一个'后缀来定义一个新的但是差别比较小的函数
doubleSmallNumber' x = (if x > 100 then x else x*2) + 1

In [33]:
-- 函数不能以大写字母开头
-- 没有参数的函数被称为定义或名字
conanO'Brien = "It's a-me, Conan O'Brien!"


Use camelCase
Found:
conanO'Brien = ...
Why Not:
conanOBrien = ...

In [34]:
let lostNumbers = [4, 8, 15, 16, 23, 42]
lostNumbers


[4,8,15,16,23,42]

In [35]:
[1, 2, 3, 4] ++ [9, 10, 11, 12]


[1,2,3,4,9,10,11,12]

In [37]:
"hello" ++ " " ++ "world"


"hello world"

In [38]:
-- 单引号为字符,双引号为字符串
'A': " Small CAT"


"A Small CAT"

In [39]:
5:[1,2,3,4,5]


[5,1,2,3,4,5]

In [40]:
-- 列表智能防同一类内容
[1,2,3,4,5] ++ []


[1,2,3,4,5]

In [41]:
-- 列表索引使用 !! 操作符
"Steve Buscemi" !! 6


'B'

In [42]:
[9.4, 33.2, 98.2, 11.2, 23.21] !! 1


33.2

In [43]:
-- 嵌套列表长度可以不同,但类型必须一样
let b = [[1,2,3,4], [5,3,3,3],[1,2,3,4,5], [1,2,3]]
b


[[1,2,3,4],[5,3,3,3],[1,2,3,4,5],[1,2,3]]

In [44]:
b ++ [[1,1,1,1]]


[[1,2,3,4],[5,3,3,3],[1,2,3,4,5],[1,2,3],[1,1,1,1]]

In [45]:
[6,6,6]:b


[[6,6,6],[1,2,3,4],[5,3,3,3],[1,2,3,4,5],[1,2,3]]

In [46]:
b !! 2


[1,2,3,4,5]

In [47]:
-- 列表是可以比较的,按顺序比较列表内元素
[3,2,1] > [2, 1, 0]


True

In [48]:
[3,2,1] > [2, 10, 100]


True

In [49]:
[3,4,2] > [3,4,3]


False

In [50]:
[3,4,2] == [3,4,2]


True

In [51]:
-- 更多列表操作
head [5, 4, 3, 2, 1]


5

In [52]:
tail [5, 4, 3, 2, 1]


[4,3,2,1]

In [53]:
last [5, 4, 3, 2, 1]


1

In [54]:
init [5, 4, 3, 2, 1]


[5,4,3,2]

In [55]:
-- this would raise
-- head []

In [56]:
length [5, 4, 3, 2, 1]


5

In [57]:
-- if null
null [1, 2, 3]


False

In [58]:
null []


Evaluate
Found:
null []
Why Not:
True
True

In [59]:
reverse [5 ,4, 3, 2, 1]


[1,2,3,4,5]

In [60]:
take 3 [5,4,3,2,1]


[5,4,3]

In [61]:
take 1 [3, 9, 3]


[3]

In [62]:
take 5 [1, 2]


[1,2]

In [63]:
take 0 [6, 6, 6]


Take on a non-positive
Found:
take 0 [6, 6, 6]
Why Not:
[]
[]

In [64]:
drop 3 [8,4, 2, 15,6]


[15,6]

In [65]:
drop 0 [1,2,3,4]


[1,2,3,4]

In [66]:
drop 100 [1,2,3,4]


[]

In [67]:
maximum [1,9,2,3,4]


9

In [68]:
minimum [8,4,2,1,5,6]


1

In [69]:
sum [5,2,1,6,3,2,5,7]


31

In [70]:
product [6,2,1,2]


24

In [71]:
product [1,2,5,6,7,9,3,0]


0

In [72]:
-- 使用 `` 把前缀函数变成后缀函数
4 `elem` [3,4,5,6]
10 `elem` [3,4,5,6]


True
False

In [73]:
--  区间
[1..20]


[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

In [74]:
['a'..'z']


"abcdefghijklmnopqrstuvwxyz"

In [76]:
['K'..'Z']


"KLMNOPQRSTUVWXYZ"

In [77]:
[2,4..20]


[2,4,6,8,10,12,14,16,18,20]

In [78]:
[3,6..20]


[3,6,9,12,15,18]

In [79]:
[13,26..24*13]


[13,26,39,52,65,78,91,104,117,130,143,156,169,182,195,208,221,234,247,260,273,286,299,312]

In [80]:
-- haskell是惰性执行的。随你可以创建一个无线区间,在需要时取相应长度的数
take 24 [13,26..]


[13,26,39,52,65,78,91,104,117,130,143,156,169,182,195,208,221,234,247,260,273,286,299,312]

In [81]:
-- cycle/repeat/replicate用来创建无限列表
take 10 (cycle [1,2,3])


[1,2,3,1,2,3,1,2,3,1]

In [82]:
take 10 (cycle "LOL ")


"LOL LOL LO"

In [83]:
take 10 (repeat 5)


Use replicate
Found:
take 10 (repeat 5)
Why Not:
replicate 10 5
[5,5,5,5,5,5,5,5,5,5]

In [84]:
replicate 3 10


[10,10,10]

In [85]:
-- haskell浮点数时不准确的
[0.1,0.3..1]


[0.1,0.3,0.5,0.7,0.8999999999999999,1.0999999999999999]

In [86]:
-- 列表推导
[x*2 | x <- [1..10]]


[2,4,6,8,10,12,14,16,18,20]

In [87]:
[x*2 | x <- [1..10], x*2 >= 12]


[12,14,16,18,20]

In [88]:
[x | x <- [50..100], x `mod` 7 == 3]


[52,59,66,73,80,87,94]

In [90]:
let booBangs xs = [if x < 10 then "BOOM!" else "BANG!" | x <- xs, odd x]

In [91]:
booBangs [7..13]


["BOOM!","BOOM!","BANG!","BANG!"]

In [92]:
[x | x <- [10..20], x /= 13, x/=15, x/=19]


[10,11,12,14,16,17,18,20]

In [93]:
[x + y | x <- [1,2,3], y <- [10, 100, 1000]]


[11,101,1001,12,102,1002,13,103,1003]

In [94]:
[x + y | x <- [1,2,3], y <- [10, 100, 1000, 10]]


[11,101,1001,11,12,102,1002,12,13,103,1003,13]

In [95]:
[x*y | x <- [2,5,10], y <- [8,10,11], x*y > 50]


[55,80,100,110]

In [97]:
-- 通过列表推导编写length函数;
let length' xs = sum [1 | _ <- xs]

In [99]:
let removeNonUppercase st = [c | c <- st, c `elem` ['A'..'Z']]

In [100]:
-- 元组是固定长度的可异构的集合
(1, 3)


(1,3)

In [101]:
(3, 'a', "hello")


(3,'a',"hello")

In [102]:
-- 长度为2的元组又叫序对,长度为3的叫triple
-- 不同长度的元组是不同类型
-- 不能创建单元素列表

In [104]:
-- 使用pair
fst (3, 1)


3

In [105]:
fst ("wow", False)


"wow"

In [106]:
snd (8, 11)


11

In [107]:
snd ("wow", False)


Evaluate
Found:
snd ("wow", False)
Why Not:
False
False

In [108]:
zip [1, 2, 3, 4] [5,5,5,5,5]


[(1,5),(2,5),(3,5),(4,5)]

In [110]:
let rightTriangle = [(a, b, c) | c <- [1..10], a <- [1..c], b <- [1..c], a^2 + b^2 == c^2, a+ b+ c == 24]

In [111]:
rightTriangle


[(6,8,10),(8,6,10)]

In [ ]: