http://www.ibm.com/developerworks/cn/java/j-cb07186.html

1. 构造符号

:

比如:

1:2:3:[]

而常用的

[1,2,3]

是一种语法糖(http://en.wikipedia.org/wiki/Syntactic_sugar

2. 一切都是函数

函数定义语法:

函数名 :: 参数类型中可能用到的基本类型 基本类型名 => 参数1 -> 参数2 -> 返回值类型

函数名 模式1 = 实现1

函数名 模式2 = 实现2

......

3. 从集合中取出元素

元素 <- 集合

4. 定义为

名称 :: 定义

5. 模式

基本类型

a b

Tuple

a b
(x1, y1)(x2, y2)

List

x:xs这样的模式可以将list的头部绑定为x,尾部绑定为xs。如果这list只有一个元素,那么xs就是一个空list。

http://fleurer-lee.com/lyah/syntax-on-function.htm

[]
(x:xs)
(x:_)
(x:y:[])


类型

When we write a type explicitly, we use the notation

expression :: MyType

to say that  expressionhas the type MyType.

As you can see, we can apply head and tail to lists of different types. Applying head to
a [Char] value returns a Char value, while applying it to a [Bool] value returns a Bool
value. The head function doesn’t care what type of list it deals with.
Because the values in a list can have any type, we call the list type polymorphic.
When we want to write a polymorphic type, we use a type variable, which must begin with a
lowercase letter. A type variable is a placeholder, where we’ll eventually substitute a
real type.

Haskell实际上代表了一种方向,即编程并不需要关注How,而是关注What。如果Haskell能够流行起来,或者更加简化,或者更加强大,那么很多程序员本身的价值就会褪色。

a type name, and hence a type constructor, must start with
a capital letter.

Defining a New Data Type

data BookInfo = Book Int String [String]
deriving (Show)

BookInfo是Type的名称,而Book是Value名称,听上去很不可思议,说白了,BookInfo是系统用的名称,而Book是给用户用的名称。

ghci> :type myInfo
myInfo :: BookInfo

或者说,可以用Book来定义一个数据,但是它的实际类型是BookInfo.

也可以让二者拥有相同的名字

data Book = Book Int String [String]
deriving (Show)

The :info command gets ghcito tell us everything it knows about a name:

下面的定义类似于 typedef

type CustomerID = Int
type ReviewBody = String
data BetterReview = BetterReview BookInfo CustomerID ReviewBody

下面关于Bool的定义也可以让我们豁然开朗

data Bool = False | True


尝试写一些常用的函数

Fibonacci number

http://en.wikipedia.org/wiki/Fibonacci_number

fabonacci :: (Integral a) => a -> a
fabonacci 0 = 1
fabonacci 1 = 1
fabonacci n = fabonacci(n - 2) + fabonacci(n - 1)

保存为fabonacci.hs, 在命令行中执行:

H:\haskell>ghci
GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :l fabonacci.hs
[1 of 1] Compiling Main ( fabonacci.hs, interpreted )
Ok, modules loaded: Main.
*Main> map fabonacci [1..20]
[1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946]

对数组执行幂操作

pow :: (Integral a) => a -> a -> a

pow 0 y = 1

pow x 0 = 1

pow x 1 = x

pow x y = x * (pow x (y-1))

 

power :: (Integral a) => a -> [a] -> [a]

power y xs = map (\x -> pow x y) xs 

执行结果

*Main> power 3 [1..10]

[1,8,27,64,125,216,343,512,729,1000]

*Main> power 4 [1..10]

[1,16,81,256,625,1296,2401,4096,6561,10000]

*Main> power 5 [1..10]

[1,32,243,1024,3125,7776,16807,32768,59049,100000]

*Main> power 6 [1..10]

[1,64,729,4096,15625,46656,117649,262144,531441,1000000]

*Main> power 7 [1..10]

[1,128,2187,16384,78125,279936,823543,2097152,4782969,10000000]

*Main> power 8 [1..10]

[1,256,6561,65536,390625,1679616,5764801,16777216,43046721,100000000]

*Main> power 9 [1..10]

[1,512,19683,262144,1953125,10077696,40353607,134217728,387420489,1000000000]

*Main> power 10 [1..10]

[1,1024,59049,1048576,9765625,60466176,282475249,1073741824,3486784401,100000000

00]

当Haskell与数学结合在一起的时候,真是威力无穷!

查找100以内的所有勾股数组合

http://zh.wikipedia.org/wiki/%E7%95%A2%E9%81%94%E5%93%A5%E6%8B%89%E6%96%AF%E4%B8%89%E5%85%83%E6%95%B8

通过List Comprehension的方式,可以很快地完成

Prelude> [(x, y, z)| x <- [1..100], y <- [1..x], z <- [1..y], x*x == y*y + z*z]

[(5,4,3),(10,8,6),(13,12,5),(15,12,9),(17,15,8),(20,16,12),(25,20,15),(25,24,7)

(26,24,10),(29,21,20),(30,24,18),(34,30,16),(35,28,21),(37,35,12),(39,36,15),(4

,32,24),(41,40,9),(45,36,27),(50,40,30),(50,48,14),(51,45,24),(52,48,20),(53,45

28),(55,44,33),(58,42,40),(60,48,36),(61,60,11),(65,52,39),(65,56,33),(65,60,25

,(65,63,16),(68,60,32),(70,56,42),(73,55,48),(74,70,24),(75,60,45),(75,72,21),(

8,72,30),(80,64,48),(82,80,18),(85,68,51),(85,75,40),(85,77,36),(85,84,13),(87,

3,60),(89,80,39),(90,72,54),(91,84,35),(95,76,57),(97,72,65),(100,80,60),(100,9

,28)]

怎么用函数写呢?

Haskell语法的更多相关文章

  1. Python入门笔记(13):列表解析

    一.列表解析 列表解析来自函数式编程语言(haskell),语法如下: [expr for iter_var in iterable] [expr for iter_var in iterable i ...

  2. 可爱的 Python : Python中的函数式编程,第三部分

    英文原文:Charming Python: Functional programming in Python, Part 3,翻译:开源中国 摘要:  作者David Mertz在其文章<可爱的 ...

  3. 《转》python学习(12)-列表解析

    转自 http://www.cnblogs.com/BeginMan/p/3164937.html 一.列表解析 列表解析来自函数式编程语言(haskell),语法如下: [expr for iter ...

  4. 《转》python 12 列表解析

    转自 http://www.cnblogs.com/BeginMan/p/3164937.html 一.列表解析 列表解析来自函数式编程语言(haskell),语法如下: [expr for iter ...

  5. Haskell函数的语法

    本章讲的就是 Haskell 那套独特的语法结构,先从模式匹配开始.模式匹配通过检查数据的特定结构来检查其是否匹配,并按模式从中取得数据. 在定义函数时,你可以为不同的模式分别定义函数本身,这就让代码 ...

  6. haskell学习笔记<1>--基本语法

    七月记录:整个七月就在玩,参加夏令营,去遨游.... 八月份需要开始复习,正等书的这个过程突然想起一直没有完成的学习-haskell,所以当前的目标是用haskell制作一个局域网通信的小工具,要求: ...

  7. haskell基本语法

    定义新类型 data EmployeeInfo = Employee Int String String [String] deriving(Read, Show, Eq, Ord) Employee ...

  8. Markdown编辑器语法指南2

    人的一切痛苦, 本质上都是对自己的无能的愤怒. --王小波 1 Markdown编辑器的基本用法 1.1 代码 如果你只想高亮语句中的某个函数名或关键字,可以使用 `function_name()` ...

  9. MorkDown 常用语法总结

    推荐一款很好用的markdown编辑器:http://www.typora.io/ 基本技巧: 代码高亮 如果你只想高亮语句中的某个函数名或关键字,可以使用``实现 通常编辑器根据diamagneti ...

随机推荐

  1. centos7 rm -rf 删除文件的找回

    今天手贱一不小心把我正个项目目录删除了,没办法只能找回啦      文件系统是ext4的,只能使用extundelete   df -hT 文件名  查看 下删除的文件夹所在的硬盘 1. 安装依赖   ...

  2. Hive 窗口函数

    举例: row_number() over(partition by clue_id order by state_updated desc) 业务举例: select distinct a.clue ...

  3. FATFS模块应用笔记

    FATFS模块应用笔记 如何港 范围 内存使用 模块尺寸缩小 长文件名 统一的API 重入 复制文件访问 性能有效文件访问 对闪存介质考虑 关键的第 延长使用FATFS API 关于FATFS许可证 ...

  4. 关于radio选中或者反选

      关注点:一.attr()和prop()的区别 <!DOCTYPE html> <html> <head> <title>JavaScript对文字按 ...

  5. linux安装相关软件

    XShell上传jdk文件到Linux并安装配置1.yum -y install lrzsz2.sudo rz选文件3.sudo tar -zxvf jdk-8u131-linux-x64.tar.g ...

  6. PHP面试 MySQL创建高性能索引考点

    MySQL索引 MySQL索引的基础和类型 索引的基础:索引类似于书籍的目录,要想找到一本书的某个特定篇章,需要查找书的目录,定位对应的页码 存储引擎使用类似的方式进行数据查询,先去索引当中找到对应的 ...

  7. SPSS转换菜单:创建时间序列

    SPSS转换菜单:创建时间序列 1.概念:"创建时间序列"对话框允许您基于现有数值型时间序列变量的函数创建新的变量.这些转换后的值在时间序列分析中非常有用. 2.操作:转换-创建时 ...

  8. SQLServer 用法简例

      子曰:“温故而知新,可以为师矣.”孔子说:“温习旧知识从而得知新的理解与体会,凭借这一点就可以成为老师了.“ 尤其是咱们搞程序的人,不管是不是全栈工程师,都是集十八般武艺于一身.不过有时候有些知识 ...

  9. vue实现轮播效果

    vue实现轮播效果 效果如下:(不好意思,图有点大:) 功能:点击左侧图片,右侧出现相应的图片:同时左侧边框变颜色. 代码如下:(也可以直接下载文件) <!DOCTYPE html> &l ...

  10. 转 Python - openpyxl 读写操作Excel

    Python - openpyxl 读写操作Excel   openpyxl特点   openpyxl(可读写excel表)专门处理Excel2007及以上版本产生的xlsx文件,xls和xlsx之间 ...