為毛要寫這個呢... 因為本沙茶除了做算法題和會那么一丁丁丁丁點的Linux基本操作就什么都不會了...
所以... 不知道為毛就要學這個了... 神經病發(fā)作?
但是figo說要學那就一定學了..........................
1 ghci>4/3
2 1.3333333333333333
3 ghci> 1+1
4 2
5 ghci>1000 *10000000
6 10000000000
7 ghci>34 ^ 28
8 7610438437126150739469436175738091335581696
9 ghci> 2 --2
10 2
11 ghci>2-(-2)
12 4
13 ghci>(-) 2 2
14 0
需要注意的一些地方:
1. 整數(shù)/整數(shù) 自動轉為浮點數(shù)...
1 ghci>(4/2) ^2
2 4.0
3 ghci>2 ^ 2
4 4
上面是證據(jù)...
2. 負數(shù)要加括號
3. 算數(shù)操作符可以當做函數(shù)使用(以后再討論)
4. 自動處理高精度
Boolean Logic, Operators, and Value Comparisons
邏輯與 &&
邏輯或 ||
邏輯非 not
1 ghci> True && False
2 False
3 ghci> True || False
4 True
5 ghci> not True || False
6 False
7 ghci> not True || True
8 True
9 ghci> not (True || True)
10 False
注意not的修飾范圍...
與C語言不同的是: 其他類型不可以自動轉化為邏輯類型, 這一點和Java是一樣的...
比較運算符有 > < >= <= 和/= 也就是不等于
1 ghci> True && False
2 False
3 ghci> True || False
4 True
5 ghci> not True || False
6 False
7 ghci> not True || True
8 True
9 ghci> not (True || True)
10 False
Operator Precedence and Associativity
用info可以查看運算符的優(yōu)先級
1 ghci>:info (+)
2 class Num a where
3 (+) :: a -> a -> a
4
5 -- Defined
in `GHC.Num'
6 infixl 6 +
7 ghci>:info (*)
8 class Num a where
9
10 (*) :: a -> a -> a
11
12 -- Defined
in `GHC.Num'
13 infixl 7 *
14 ghci>:info (^)
15 (^) :: (Num a, Integral b) => a -> b -> a -- Defined
in `GHC.Real'
16 infixr 8 ^
Lists and Operators on Lists
和python不同,Haskell的數(shù)組里面存放的元素類型必須都是相同的!
1 ghci>[1,2,3,4]
2 [1,2,3,4]
3 ghci>[1,2,3,4,"figo"]
4 5 <interactive>:77:2:
6 No instance
for (Num [Char])
7 arising from the literal `1'
8 Possible fix: add an instance declaration
for (Num [Char])
9 In the expression: 1
10 In the expression: [1, 2, 3, 4,

.]
11 In an equation
for `it': it = [1, 2, 3,

.]
12 ghci>["figo","hanfei19910905"]
13 ["figo","hanfei19910905"]
14 可以通過枚舉來定義數(shù)組
1 ghci>[1,4,8..32]
2
3 <interactive>:84:7: parse error on input `..'
4 ghci>[1,2..10]
5 [1,2,3,4,5,6,7,8,9,10]
6 ghci>[1,4..10]
7 [1,4,7,10]
8 ghci>[1,4..9]
9 [1,4,7]
10 ghci>[10,9..1]
11 [10,9,8,7,6,5,4,3,2,1]
合并兩個數(shù)組
1 ghci>[[1,1],[2,2]] ++ [[1,1,1]]
2 [[1,1],[2,2],[1,1,1]]
3 ghci>["figo"] ++ ["string"]
4 ["figo","string"]
字符串,也就是字符數(shù)組
1 ghci>'a':"bc"
2 "abc"
3 ghci>['f','i','g','0']
4 "fig0"
posted on 2012-05-15 14:28
西月弦 閱讀(1686)
評論(4) 編輯 收藏 引用 所屬分類:
讀書筆記(Haskell)