---
title: "real world haskell -读书笔记"
author: "dogstar"
date: "2009-05-21T20:19:15Z"
tags: []
slug: "real-world-haskell"
draft: false
lastmod: "2009-06-26T21:02:12Z"
id: 393027
url: "https://dogstar.iteye.com/blog/393027"
---

```haskell

Prelude> :type map
map :: (a -> b) -> [a] -> [b]

```


 -> 右结合 所以 (a -> b) ->  -> ** == ((a -> b) ->(  -> **))
(a->b) 输入为a类型,返回b类型的函数A;
(  -> **)) 输入为类型,返回**类型的函数B;
((a -> b) ->(  -> **)) 输入为函数A,返回为函数B
至此map就是一个输入为函数A,返回为函数B的函数.

实例: map (\(x) -> x*2)  == (map (\(x) -> x*2))  加上函数的currying上面的就好理解了.

oop容易跌入状态同步的泥潭,fp容易跌入函数传来传去的泥潭 :)


Char -> 'a'
String -> "aaaa" ===  / 

list ->  不固定长度,同类型.
Tuple -> ("asdf",1,2,4) 初始化好后不可改变.

函数调用左集合 a b c d = (((a b) c) d)

*x = 10
x = 11 can't 不能两次给变量赋值 除了类型推导,怎么越看越像erlang :),*



```java
: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--------


```
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))
```



> foldl顾名思义从左边,向右卷起来. step zero x
> foldr顾名思义从右边,向左卷起来. step x zero
> zero位置和方向一致.


In fact, we can even write foldl using foldr!

----------4.6章节

  :type dropWhile
dropWhile :: (a -> Bool) ->  -> 

                  haskell的function都是单参数.类型标注是右结合.所以如下:

a -> Bool) ->  ->  = ((a -> Bool) -> ( -> ))
意思是:接收一个函数(a -> Bool) ,返回一个函数  -> 

xs@(_:xs') = "hello"   --------- xs等于hello,xs'等于ello

like erlang (X=)="hello"