Prelude> :type map
map :: (a -> b) -> [a] -> [b]
[list]
[*] -> 右结合 所以 (a -> b) -> [a] -> [b] == ((a -> b) ->( [a] -> [b]))
[*](a->b) 输入为a类型,返回b类型的函数A;
[*]( [a] -> [b])) 输入为[a]类型,返回[b]类型的函数B;
[*]((a -> b) ->( [a] -> [b])) 输入为函数A,返回为函数B
[*]至此map就是一个输入为函数A,返回为函数B的函数.
[/list]
实例: map (\(x) -> x*2) [1,2] == (map (\(x) -> x*2)) [1,2] 加上函数的currying上面的就好理解了.
oop容易跌入状态同步的泥潭,fp容易跌入函数传来传去的泥潭 :)
Char -> 'a'
String -> "aaaa" === ['a','a','a','a'] / [Char]
list -> [] 不固定长度,同类型.
Tuple -> ("asdf",1,2,4) 初始化好后不可改变.
函数调用左集合 a b c d = (((a b) c) d)
[i]x = 10
x = 11 can't 不能两次给变量赋值 除了类型推导,怎么越看越像erlang :),[/i]
:type take
take::Int -> [a] -> [a] === take:Int -> ([a]->[a])
currying
take 3 [1,2,4,5] 看似多参数,其实是:((take 3) [1,2,,4,5]) = [1,2,4]
Defining a New Data Type
haskell 原生的也就是list,tuple (erlang也一样).但是haskell可自己定义data type.erlang只能用装成record的tuple.
haskell玩来玩去骨子里也是tuple,但是他封装的好,有类型,有模有样.比erlang强太多.
-------foldl,foldr--------
[code]foldl (+) 0 (1:2:3:[])
== foldl (+) (0 + 1) (2:3:[])
== foldl (+) ((0 + 1) + 2) (3:[])
== foldl (+) (((0 + 1) + 2) + 3) []
== (((0 + 1) + 2) + 3)
foldr (+) 0 (1:2:3:[])
== 1 + foldr (+) 0 (2:3:[])
== 1 + (2 + foldr (+) 0 (3:[])
== 1 + (2 + (3 + foldr (+) 0 []))
== 1 + (2 + (3 + 0))[/code]
[quote]foldl顾名思义从左边,向右卷起来. step zero x
foldr顾名思义从右边,向左卷起来. step x zero
zero位置和方向一致.[/quote]
In fact, we can even write foldl using foldr!
----------4.6章节
:type dropWhile
dropWhile :: (a -> Bool) -> [a] -> [a]
haskell的function都是单参数.类型标注是右结合.所以如下:
a -> Bool) -> [a] -> [a] = ((a -> Bool) -> ([a] -> [a]))
意思是:接收一个函数(a -> Bool) ,返回一个函数 [a] -> [a]
xs@(_:xs') = "hello" --------- xs等于hello,xs'等于ello
like erlang (X=[_|T])="hello"