Programming In Haskell 读书笔记1

书里面用的是huge我用的是GHC

1 首先到 http://haskell.org/ghc/ 这边去下载个haskell的 编译器。
如果不习惯emacs的话可以到[url=http://eclipsefp.sourceforge.net/haskell/index.html]这里[/url] 去下个 eclipse的插件,不过要注意这个插件只支持jdk5.0.

2 haskell 中有+ - * / ^ 和 `div` (这个是只取整数部分,比如 5 'div'3 输出 1 其他的符号就不用解释了。
这边要注意一些算符优先级, 比如2*3^4 相当于 2*(3^4) ,而2^3^4相当于 2^(3^4)。

3 haskell中还有很多操作list的操作。
[code]
(1) 选择一个非空lists中的第一个元素
head [1,2,3,4,5] => 1
(2)从一个非空list移除第一个元素
tail [1,2,3,4,5] =>[2,3,4,5]
(3) 选择list的第N个元素
[1,2,3,4,5] !!2 => 3
(4) 选择list中的前N个元素
take 3[1,2,3,4,5] =>[1,2,3]
(5) 删除list中前N个元素
drop 3[1,2,3,4,5] =>[4,5]
(6)计算一个list的长度
   length [1,2,3,4,5] =>5
(7)计算一个list的和
sum [1,2,3,4,5]=>15
(8)计算一个list的积
product[1,2,3,4,5] =>120
(9)合并两个list
[1,2,3]++[4,5] =>[1,2,3,4,5]
(10)反转一个list
reverse[1,2,3,4,5] =>[5,4,3,2,1]
[/code]

4 数学中的表达式和haskell的关系
比如数学中 的一个表达式 f(a,b) +cd 表示一个函数f(a,b)的值加上cd的积的值。而在haskell中我们是这样写的 f a b +c*d
下面这张表是数学中的表达式表示和haskell中的区别.
[code]
Mathematics Haskell
f(x) f x
f(x,y) f x y
f(g(x)) f (g x)
f(x,g(y)) f x (g y)
f(x)g(y) f x *g y
[/code]

5 然后就可以写第一个haskell程序了。
(1)首先在 任意一个目录下 比如 d:/ 建立一个 文件first.hs. 编辑这个文件 然后键入
[code]double x=x+x
quadruple x=double(double x)
[/code]
然后启动GHCI ,然后键入 :CD D:/ 这样就进入了 D盘根目录下,然后 再键入 :load first 如果load成功它会显示 OK,modules loaded:Main.

然后你输入 double 5 它会显示 10 输入quadruple 10 它会显示 40。
(2)我们再在d:/下建一个文件second.hs 编辑键入
[code]
factorial n = product [1..n]
average ns = sum ns `div` length ns
[/code]
然后 :load second ,load成功后,在GHCI中键入
factorial 10它会 输出 36288000
键入 average [1,2,3,4,5] 他会输出 3

想要了解GHC更多的命令的话,可以去查他的DOC或者直接在GHCI中键入 :?

6 要注意haskell中它的函数名必须得以一个小写字母开头,不然就会报错,下面是 haskell中关键字
[quote]
case class data default deriving do else
if import in infix infixl infixr instance
let module new type of then type where[/quote]

这边解释一下where 关键字 ,给个例子大家就明白了
[code]
a= b+c
where
{b=1;
c=2}
d=a*2[/code]
load之后,在GHCi中键入a 就会输出3,键入d就会输出6,呵呵,大家这下明白了吧.

7 在haskell中注释分两种,单行注释是 --,多行的话 {- 与-}

呵呵,欢迎大家来拍砖.
看到很多版本都是没书签的,所以就自己添了书签造福一下大家 小小劳动成果,还请尊重,若有错误还请多指教w Abstraction: Programming in Haskell Second Edition Haskell is a purely functional language that allows programmers to rapidly develop clear, concise and correct software. The language has grown in popularity in recent years, both in teaching and in industry. This book is based on the author’s experience of teaching Haskell for more than 20 years. All concepts are explained from first principles and no programming experience isrequired, making this book accessible to a broad spectrum of readers. While Part I focuses on basic concepts, Part II introduces the reader to more advanced topics. This new edition has been extensively updated and expanded to include recent and more advanced features of Haskell, new examples and exercises, selected solutions, and freely downloadable lecture slides and code. The presentation is clean and simple, while also being fully compliant with the latest version of the language, including recent changes concerning applicative, monadic, foldable and traversable types. GRAHAM HUTTON is Professor of Computer Science at the University of Nottingham. He has taught Haskell to thousands of students and received numerous best lecturer awards. Hutton has served as an editor of the Journal of Functional Programming, chair of the Haskell Symposium and the International Conference on Functional Programming, vice-chair of the ACM Special Interest Group on Programming Languages, and he is an ACM Distinguished Scientist.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值