「学习总结-Haskell-1」Haskell 基础知识
2013-01-31 23:05
721 查看
Haskell基础知识
Table of Contents
1 Haskell 基本知识1.1 Haskell 开发环境安装
1.2 Haskell 代码如何运行
1.3 Haskell 函数定义和使用
1.4 Haskell 基本语句和编程模式
1 Haskell 基础知识
1.1 Haskell 开发环境安装
见《Haskell Platform安装过程》1.2 Haskell 代码如何运行
代码-- helloWorld.hs -- 定义名为hi的函数 hi = "hello, world!"
运行
$ ghci GHCi, version 7.4.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. ghci>:l helloWorld.hs [1 of 1] Compiling Main ( helloWorld.hs, interpreted ) Ok, modules loaded: Main. ghci>hi "hello, world!"
1.3 Haskell 函数定义和使用
-- function.hs doubleMe x = x + x doubleUs x y = doubleMe x + doubleMe y
ghci>:l function.hs [1 of 1] Compiling Main ( function.hs, interpreted ) Ok, modules loaded: Main. ghci>doubleMe 2 4 ghci>doubleUs 3 4 14
1.4 Haskell 基本语句和编程模式
if语句maxNumber x y = if x > y then x else y
ghci>:l baseStruc.hs [1 of 1] Compiling Main ( baseStruc.hs, interpreted ) Ok, modules loaded: Main. ghci>maxNumber 3 5 5 ghci>maxNumber 5 3 5
case语句
isLucky x = case x of 1 -> "1:lucky" 2 -> "2:No lucky" otherwise -> "lucky"
ghci>isLucky 1 "1:lucky" ghci>isLucky 2 "2:No lucky" ghci>isLucky 3 "lucky" ghci>isLucky 4 "lucky"
let语句
doubleSum x y = let doublex = 2 * x doubley = 2 * y in doublex + doubley
ghci>doubleSum 3 4 14 ghci>doubleSum 1 2 6
where语句
doubleSum2 x y = doublex + doubley where doublex = 2 * x doubley = 2 * y
ghci>doubleSum2 1 2 6 ghci>doubleSum2 2 3 10
模式匹配(Pattern Matching)
例1
lucky 7 = "You are lucky" lucky x = "Sorry, you are not lucky"
ghci>lucky 7 "You are lucky" ghci>lucky 1 "Sorry, you are not lucky" ghci>lucky 2 "Sorry, you are not lucky"
例2
foo 0 x = (-1) * x foo 1 x = x foo x y = 0
ghci>foo 1 2 2 ghci>foo 2 3 0 ghci>foo 3 4 0 ghci>foo 0 2 -2
例3
foo' 0 x = (-1) * x foo' x y = 0 foo' 1 x = x
ghci>foo' 1 2 0 ghci>foo' 1 3 0 ghci>foo' 2 3 0 ghci>foo' 3 4 0 ghci>foo' 0 2 -2
门卫(Guard)
例1
mdivide x y | y == 0 = "Can not divide" | x / y > 10 = "first number is larger than the second number" | x / y < 1 = "first number is less than the second number" | otherwise = "almost equal"
ghci>mdivide 3 0 "Can not divide" ghci>mdivide 3 2 "almost equal" ghci>mdivide 30 2 "first number is larger than the second number" ghci>mdivide 3 5 "first number is less than the second number"
例2
mdivide' x y | x / y > 10 = "first number is larger than the second number" | y == 0 = "Can not divide" | x / y < 1 = "first number is less than the second number" | otherwise = "almost equal"
ghci>mdivide' 3 0 "first number is larger than the second number" ghci>mdivide' 3 1 "almost equal" ghci>mdivide' 30 3 "almost equal" ghci>mdivide' 30 2 "first number is larger than the second number" ghci>mdivide' 2 3 "first number is less than the second number"
递归(Recursion)
注:理解门卫与递归如何结合在一起工作
msum x y | x > y = msum y x | x == y = x | otherwise = x + msum (x+1) y
ghci>msum 1 100 5050 ghci>msum 2 4 9 ghci>msum 100 1 5050
相关文章推荐
- 【SQLServer】T-SQL查询学习总结(2)[基础知识]
- 进程学习总结之进程的基础知识和创建进程
- c++学习历程(3)之 第二章基础知识总结
- Python 学习总结(一):掌握基础知识,查缺补漏
- [置顶] Java基础知识学习总结链接
- OpenGL ES学习总结一:基础知识简介
- Java基础知识学习总结---(java连接Oracle数据库)
- java基础知识学习与总结(一)
- 进程学习总结之进程的基础知识和创建进程
- 黑马程序员--集合框架知识总结和泛型--java学习日记7(基础知识)
- struts 入门基础知识学习总结(转)
- 黑马程序员-基础知识学习总结
- 博弈论的一些基础知识(参考网络资料,学习总结,很好,分享并保存)
- makefile学习总结-----基础知识
- jQuery学习总结之基础知识----持续更新中
- SQL Server 基础知识学习总结01
- 博弈论的一些基础知识(参考网络资料,学习总结,很好,分享并保存)
- CSS3基础知识学习总结笔记
- 黑马程序员学习log第六篇基础知识:JAVA的面向对象之IO总结
- Java基础学习总结(97)——合格的Java的架构师需要具备的技术知识