Haskell語法

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

1. 構造符號

:

好比:java

1:2:3:[]

而經常使用的程序員

[1,2,3]

是一種語法糖(http://en.wikipedia.org/wiki/Syntactic_sugarexpress

 

2. 一切都是函數

函數定義語法:編程

函數名 :: 參數類型中可能用到的基本類型 基本類型名 => 參數1 -> 參數2 -> 返回值類型
函數名 模式1 = 實現1
函數名 模式2 = 實現2
......

 

3. 從集合中取出元素

元素 <- 集合

4. 定義爲

名稱 :: 定義

5. 模式

基本類型api

a b


Tuple數組

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

Listapp

x:xs這樣的模式能夠將list的頭部綁定爲x,尾部綁定爲xs。若是這list只有一個元素,那麼xs就是一個空list。函數

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

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

怎麼用函數寫呢?

相關文章
相關標籤/搜索