算法学社
記錄難忘的征途
posts - 141,comments - 220,trackbacks - 0

Haskell's Type System

    Haskell的数据类型有三个特点:
    1. 强类型
    2. 静态类型
    3. 可自动推断
    额,我按照自己的理解解释一下吧...
    强类型不支持自动类型转换,为了让代码可靠一些...
    静态类型是每个表达式或者变量的类型都是静态不变的...
    Haskell可以自动检测表达式中常数的数据类型...

common basic types

   char: Unicode字符
   bool:True & False
   Int : 在32位机器上是32位,在64位机器上是64位
   Integer: 高精度整形
   Double:64位浮点型

Function Application

一些函数需要注意的地方,就是如果你要把一个函数的值传到另一个函数里,需要加一个括号以改变优先级
 1 ghci>odd 3
 2 True
 3 ghci>even 6
 4 True
 5 ghci>compare (sqrt 2) 1
 6 GT
 7 ghci>compare (sqrt 2) 1 == GT
 8 True
 9 ghci>compare sqrt 2 1 == GT
10 
11 <interactive>:16:1:
12     The function `compare' is applied to three arguments,
13     but its type `a0 -> a0 -> Ordering' has only two
14     In the first argument of `(==)', namely `compare sqrt 2 1'
15     In the expression: compare sqrt 2 1 == GT
16     In an equation for `it': it = compare sqrt 2 1 == GT

Composite Data Types: Lists and Tuples

字符串就是一个字符列表,head 函数可以返回列表的第一个元素,tail可以返回列表的除了第一个元素的所有元素。
1 ghci>head "figo"
2 'f'
3 ghci>tail "figo"
4 "igo"
5 ghci>tail [1,2,3]
6 [2,3]

还有另一种类型叫做“元组”。类似于C++的pair,连用法都极其相似。

ghci>snd (19910905,"hanfei")
"hanfei"
ghci>fst (19910905,"hanfei")
19910905

Function Types and Purity

:type 命令可以查看一个函数传入的参数类型和返回的参数类型
Haskell的函数只允许调用传入的参数,禁止使用全局变量。 这样的函数叫做“纯函数”。(略感坑爹)
1 ghci>:type lines
2 lines :: String -> [String]
3 ghci>lines "figo\nis\nstupid\nhahaha"
4 ["figo","is","stupid","hahaha"]

Haskell Soucre Files, and Writing Simple Functions

Haskell源文件的后缀是.hs,这样的话vim就会有语法高亮了...
定义一个函数
1 -- add.hs by figo 05.16.2012
2 add a b = a + b
我们可以用:load add.hs来加载这个函数
1 ghci>:load add.hs
2 [1 of 1] Compiling Main             ( add.hs, interpreted )
3 Ok, modules loaded: Main.
4 ghci>:type add
5 add :: Num a => a -> a -> a
6 ghci>add 1 2
7 3

Conditional Evaluation

利用if来写一个drop函数:
1 -- myDrop.hs by figo 05.16.2012
2 myDrop n xs = if n <= 0 || null xs
3             then xs
4             else myDrop (n-1)(tail xs)
里面的类型都是自动检测的... 额... 不用像C++那样提前声明
测试一下刚写的东东
1 ghci>:load myDrop.hs 
2 [1 of 1] Compiling Main             ( myDrop.hs, interpreted )
3 Ok, modules loaded: Main.
4 ghci>myDrop 2 "figo"
5 "go"
6 ghci>myDrop 1 ["hanfei","figo","hello"]
7 ["figo","hello"]

Polymorphism in Haskell

Haskell的多态性指的是,如果一个函数可以把某参数当做任意类型处理。就像C++的template一样。
ghci>:type fst
fst :: (a, b) -> a

Exercises


写一个函数,功能是返回元素的倒数第二个值。
呃... 只能写成这样子了... 好渣... 求大神指点
1 -- lastButOne.hs by figo 05.16.2012
2 lastButOne xs = if(length (xs) <= 2)
3                 then head xs
4                 else lastButOne (tail xs)
5 
posted on 2012-05-16 19:59 西月弦 阅读(1610) 评论(3)  编辑 收藏 引用 所属分类: 读书笔记(Haskell)

FeedBack:
# re: Real World Haskell 读书笔记(二) Types and Functions
2012-05-16 23:45 | OwnWaterloo
lastButOne xs = case xs of { [] -> Nothing ; y:[] -> Nothing ; x:y:[] -> Just x ; x:xs -> lastButOne xs}

或者:
lastButOne [] = Nothing
lastButOne y:[] = Nothing
lastButOne x:y:[] = Just x
lastButOne x:xs = lastButOne xs


《real world haskell》不怎么讲语言的……  回复  更多评论
  
# re: Real World Haskell 读书笔记(二) Types and Functions
2012-05-17 21:39 | 西月弦
@OwnWaterloo
多谢指教... 有没有什么比较好关于Haskell或者函数式编程的书籍推荐一下?
我是超级新手阿....  回复  更多评论
  
# re: Real World Haskell 读书笔记(二) Types and Functions
2012-05-19 11:31 | OwnWaterloo
@西月弦
Haskell我也是新手,感觉收获最大的是 http://learnyouahaskell.com/
其次是 http://www.haskell.org/tutorial/ 内容不多,但信息量很大……
real world haskell对语言的介绍根本不够看懂它里面提供的代码……

函数式编程没专门看过什么数据……

PS:cppblog的通知邮件被gmail当作垃圾了……
  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理