In [1]:
2 + 15
In [2]:
49 * 100
In [3]:
1892 - 1472
In [4]:
5 /2
In [5]:
(50 * 100) - 499
In [6]:
50 * 100 - 599
In [7]:
50 * ( 100 - 499)
In [8]:
-- 5 * -3 will raise
5 * (-3)
In [9]:
True && False
In [10]:
True && True
In [11]:
False || True
In [12]:
not False
In [13]:
not (True && True)
In [14]:
5 == 5
In [15]:
1 == 0
In [16]:
5 /= 5 -- not equal
In [17]:
5 /= 4
In [18]:
"hello" == "hello"
In [19]:
-- strong type language
-- this would raise: 5 + "hello" or 5 == True
In [20]:
-- 函数通常使用前缀调用方式.
succ 8
In [21]:
min 9 10
In [22]:
min 3.4 3.2
In [23]:
max 100 101
In [24]:
-- 函数调用有着最高的优先级
succ 9 + max 5 4 + 1
(succ 9) + (max 5 4) + 1
In [25]:
--the following was not equal
succ 9 * 10
succ (9 * 10)
In [26]:
-- 如果函数接受两个参数,可以中坠调用方式
div 92 10
92 `div` 10
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!"
In [34]:
let lostNumbers = [4, 8, 15, 16, 23, 42]
lostNumbers
In [35]:
[1, 2, 3, 4] ++ [9, 10, 11, 12]
In [37]:
"hello" ++ " " ++ "world"
In [38]:
-- 单引号为字符,双引号为字符串
'A': " Small CAT"
In [39]:
5:[1,2,3,4,5]
In [40]:
-- 列表智能防同一类内容
[1,2,3,4,5] ++ []
In [41]:
-- 列表索引使用 !! 操作符
"Steve Buscemi" !! 6
In [42]:
[9.4, 33.2, 98.2, 11.2, 23.21] !! 1
In [43]:
-- 嵌套列表长度可以不同,但类型必须一样
let b = [[1,2,3,4], [5,3,3,3],[1,2,3,4,5], [1,2,3]]
b
In [44]:
b ++ [[1,1,1,1]]
In [45]:
[6,6,6]:b
In [46]:
b !! 2
In [47]:
-- 列表是可以比较的,按顺序比较列表内元素
[3,2,1] > [2, 1, 0]
In [48]:
[3,2,1] > [2, 10, 100]
In [49]:
[3,4,2] > [3,4,3]
In [50]:
[3,4,2] == [3,4,2]
In [51]:
-- 更多列表操作
head [5, 4, 3, 2, 1]
In [52]:
tail [5, 4, 3, 2, 1]
In [53]:
last [5, 4, 3, 2, 1]
In [54]:
init [5, 4, 3, 2, 1]
In [55]:
-- this would raise
-- head []
In [56]:
length [5, 4, 3, 2, 1]
In [57]:
-- if null
null [1, 2, 3]
In [58]:
null []
In [59]:
reverse [5 ,4, 3, 2, 1]
In [60]:
take 3 [5,4,3,2,1]
In [61]:
take 1 [3, 9, 3]
In [62]:
take 5 [1, 2]
In [63]:
take 0 [6, 6, 6]
In [64]:
drop 3 [8,4, 2, 15,6]
In [65]:
drop 0 [1,2,3,4]
In [66]:
drop 100 [1,2,3,4]
In [67]:
maximum [1,9,2,3,4]
In [68]:
minimum [8,4,2,1,5,6]
In [69]:
sum [5,2,1,6,3,2,5,7]
In [70]:
product [6,2,1,2]
In [71]:
product [1,2,5,6,7,9,3,0]
In [72]:
-- 使用 `` 把前缀函数变成后缀函数
4 `elem` [3,4,5,6]
10 `elem` [3,4,5,6]
In [73]:
-- 区间
[1..20]
In [74]:
['a'..'z']
In [76]:
['K'..'Z']
In [77]:
[2,4..20]
In [78]:
[3,6..20]
In [79]:
[13,26..24*13]
In [80]:
-- haskell是惰性执行的。随你可以创建一个无线区间,在需要时取相应长度的数
take 24 [13,26..]
In [81]:
-- cycle/repeat/replicate用来创建无限列表
take 10 (cycle [1,2,3])
In [82]:
take 10 (cycle "LOL ")
In [83]:
take 10 (repeat 5)
In [84]:
replicate 3 10
In [85]:
-- haskell浮点数时不准确的
[0.1,0.3..1]
In [86]:
-- 列表推导
[x*2 | x <- [1..10]]
In [87]:
[x*2 | x <- [1..10], x*2 >= 12]
In [88]:
[x | x <- [50..100], x `mod` 7 == 3]
In [90]:
let booBangs xs = [if x < 10 then "BOOM!" else "BANG!" | x <- xs, odd x]
In [91]:
booBangs [7..13]
In [92]:
[x | x <- [10..20], x /= 13, x/=15, x/=19]
In [93]:
[x + y | x <- [1,2,3], y <- [10, 100, 1000]]
In [94]:
[x + y | x <- [1,2,3], y <- [10, 100, 1000, 10]]
In [95]:
[x*y | x <- [2,5,10], y <- [8,10,11], x*y > 50]
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)
In [101]:
(3, 'a', "hello")
In [102]:
-- 长度为2的元组又叫序对,长度为3的叫triple
-- 不同长度的元组是不同类型
-- 不能创建单元素列表
In [104]:
-- 使用pair
fst (3, 1)
In [105]:
fst ("wow", False)
In [106]:
snd (8, 11)
In [107]:
snd ("wow", False)
In [108]:
zip [1, 2, 3, 4] [5,5,5,5,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
In [ ]: