当前位置: 首页 > news >正文

Haskell语法

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

怎么用函数写呢?

转载于:https://www.cnblogs.com/long123king/p/3528363.html

相关文章:

  • CSharp面向对象设计模式纵横谈--Singleton Pattern 听课笔记
  • 5/6月学习工作总结(压力越大,意志越坚定)
  • Oracle临时表(Temporary Table)
  • 模块化开发
  • MVVM框架下,WPF实现Datagrid里的全选和选择
  • nodjs html 转 pdf
  • 动态创建HTML内容
  • 保存对象的不同状态值
  • Linux练习(write写入)
  • matlab练习程序(随机游走图像)
  • 远程桌面连接记录彻底清除
  • Android中使用WebView, WebChromeClient和WebViewClient加载网页
  • 多态的好处和弊端以及多态的理解
  • 要乐观对待生活
  • web计算器
  • 深入了解以太坊
  • .pyc 想到的一些问题
  • 2017前端实习生面试总结
  • android图片蒙层
  • CoolViewPager:即刻刷新,自定义边缘效果颜色,双向自动循环,内置垂直切换效果,想要的都在这里...
  • Docker 1.12实践:Docker Service、Stack与分布式应用捆绑包
  • gf框架之分页模块(五) - 自定义分页
  • Git 使用集
  • Logstash 参考指南(目录)
  • mysql innodb 索引使用指南
  • Mysql优化
  • Nginx 通过 Lua + Redis 实现动态封禁 IP
  • Python3爬取英雄联盟英雄皮肤大图
  • Service Worker
  • vue脚手架vue-cli
  • Vue小说阅读器(仿追书神器)
  • 创建一个Struts2项目maven 方式
  • 第2章 网络文档
  • 工作手记之html2canvas使用概述
  • 关于Flux,Vuex,Redux的思考
  • 极限编程 (Extreme Programming) - 发布计划 (Release Planning)
  • 吐槽Javascript系列二:数组中的splice和slice方法
  • 原创:新手布局福音!微信小程序使用flex的一些基础样式属性(一)
  • 原生Ajax
  • const的用法,特别是用在函数前面与后面的区别
  • LevelDB 入门 —— 全面了解 LevelDB 的功能特性
  • 如何用纯 CSS 创作一个货车 loader
  • 数据可视化之下发图实践
  • 曾刷新两项世界纪录,腾讯优图人脸检测算法 DSFD 正式开源 ...
  • 直播平台建设千万不要忘记流媒体服务器的存在 ...
  • #100天计划# 2013年9月29日
  • (4)(4.6) Triducer
  • (PyTorch)TCN和RNN/LSTM/GRU结合实现时间序列预测
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (动手学习深度学习)第13章 计算机视觉---微调
  • (六)vue-router+UI组件库
  • (深度全面解析)ChatGPT的重大更新给创业者带来了哪些红利机会
  • (转载)VS2010/MFC编程入门之三十四(菜单:VS2010菜单资源详解)
  • .mat 文件的加载与创建 矩阵变图像? ∈ Matlab 使用笔记
  • .NET CORE使用Redis分布式锁续命(续期)问题