前言
我們離不開函數(shù),到哪里都有它的身影洪己。
調(diào)用
(+ 1 2) ;; 3
(+ 1 2 3) ;; 6
(random) ;; 0.123122
+
是個(gè)函數(shù)妥凳,它可以接受兩個(gè)、三個(gè)或者更多的參數(shù)答捕。random
也是個(gè)函數(shù)猾封,它并沒有接受任何參數(shù),直接返回一值噪珊。
匿名函數(shù)
匿名函數(shù)也是函數(shù)晌缘,它跟普通函數(shù)區(qū)別在于它沒有名字,與它相對(duì)的稱為具名函數(shù)痢站。我們一般用lambda
或λ
表示磷箕,如下:
(map (lambda (x) (string-append x "!")) "hello!")
(map (λ (x) (string-append x "!") "hello!")
以上兩種寫法相同。
簡(jiǎn)寫
變量的綁定(定義):
(define var 1)
函數(shù)也是個(gè)變量阵难,所以我們可以這樣寫:
(define my-fun
(lambda (x y) (+ x x y y)))
我們將一個(gè)匿名函數(shù)綁定到my-fun上岳枷,于是它變成一個(gè)函數(shù),同樣地呜叫,我們可以使用以下簡(jiǎn)寫:
(define (my-fun x y)
(+ x x y y))
它們?cè)诖藭r(shí)(函數(shù)參數(shù)固定)可以認(rèn)為等價(jià)空繁,它們之間還是會(huì)有細(xì)微不同,下面將一一介紹朱庆。
不定個(gè)數(shù)參數(shù)
記法
比較以下兩種寫法:
(define (f/id x) x)
(define l/id (lambda x x))
按照上一節(jié)的寫法盛泡,它們所表達(dá)含義應(yīng)該是相一致。我們測(cè)試幾個(gè)例子:
> (f/id 1)
1
> (f/id '(1 2))
'(1 2)p
> (f/id 1 2)
; f/id: arity mismatch;
; the expected number of arguments does not match the given number
; expected: 1
; given: 2
; arguments...:
; 1
; 2
(l/id 1)
'(1)
> (l/id '(1 2))
'((1 2))
> (l/id 1 2)
'(1 2)
f/id
與l/id
基本相同娱颊,但在處理多個(gè)輸入值出現(xiàn)了問題傲诵,f/id
默認(rèn)會(huì)檢查參數(shù)個(gè)數(shù),而lambda卻不會(huì)這樣做箱硕。
如果我們想要定義自己的+
拴竹,直接使用lambda。
(define my-add
(lambda xs (foldl + 0 xs)))
(my-add 1 2 3) ;; 6
剩余參數(shù)
racket可以匹配多個(gè)參數(shù)剧罩,如果例如我們實(shí)現(xiàn)自己的head
栓拜。
(define my-head
(lambda (x . xs) xs))
(define (my-head x . xs) xs)
以上寫法就會(huì)相等價(jià),同樣地惠昔,我們還可以把前面多個(gè)參數(shù)一起匹配出來幕与。
(define (my-headdd a b c . xs) xs)
.
后面就是剩余下來未處理的參數(shù),它是一個(gè)list舰罚,利用這一點(diǎn)纽门,我們也可以實(shí)現(xiàn)my-add
。
(define (my-add x . xs)
(foldl + 0 (cons x xs)))
我們把x和xs再拼回到一個(gè)list营罢,之后一齊處理赏陵。
參數(shù)關(guān)鍵詞
racket函數(shù)提供關(guān)鍵詞用法饼齿,讓整個(gè)函數(shù)更加可讀。
與不定參數(shù)一樣蝙搔,我們同樣有兩種寫法缕溉。
(define my-add (lambda (z #:x x #:y y) (+ x y z)))
(define (my-add z #:x x #:y y) (+ x y z))
z是個(gè)普通參數(shù),x和y需要特殊方法輸入:
(my-add 10 #:x 1 #:y 2) ;; 13
默認(rèn)參數(shù)
不管是不定參數(shù)還是關(guān)鍵詞吃型,它們都可以提供一個(gè)默認(rèn)值证鸥。
一組默認(rèn)參數(shù),需要用方括號(hào)包圍起來勤晚。
(define (my-add [x 1] [y 1]) (+ x y))
(my-add)
2
> (my-add 2)
3
> (my-add 10 20)
30
>
(define (my-add z #:x [x 1] #:y [y 1]) (+ x y z))
(my-add 10)
12
> (my-add 10 #:x 20)
31
> (my-add 10 #:x 10 #:y 10)
30
>
Apply
在不定參數(shù)中我們用foldl
來實(shí)現(xiàn)加法運(yùn)算枉层。+
本身已經(jīng)實(shí)現(xiàn)了多參數(shù)相加,但我們的函數(shù)得到的是一個(gè)list赐写,為了能把這些參數(shù)列表運(yùn)用(apply)到+
鸟蜡,我們就可以使用apply來處理了。
(define my-add
(lambda xs (apply + xs)))
約束
racket/contract
提供了函數(shù)的約束能力挺邀,能嚴(yán)格限制一個(gè)函數(shù)的輸入輸出揉忘,例如我們想讓my-add
僅支持自然數(shù),那么我們可以這樣寫:
(define/contract (my-add x y)
(-> positive? positive? positive?)
(+ x y))
(my-add 0 1)
; my-add: contract violation
; expected: positive?
; given: 0
; in: the 1st argument of
; (-> positive? positive? positive?)
; contract from: (function my-add)
; blaming: top-level
; (assuming the contract is correct)
; at: stdin::1411-1417
>
->
算作是約束記法端铛。
不定參數(shù)約束
我們擴(kuò)展my-add
泣矛,可以讓它接受多個(gè)參數(shù),并且要求它每個(gè)參數(shù)都為自然數(shù)禾蚕。
(define/contract my-add
(->* () () #:rest (listof positive?) positive?)
(lambda xs (apply + xs)))
(my-add 1 2)
3
> (my-add 1 2 10)
13
> (my-add 1 2 0)
; my-add: contract violation
; expected: positive?
; given: 0
; in: an element of
; the rest argument of
; (->* () #:rest (listof positive?) positive?)
; contract from: (definition my-add)
; blaming: top-level
; (assuming the contract is correct)
; at: stdin::1731-1737
>
我們逐個(gè)分析->*
后面的三個(gè)參數(shù)您朽。
-
()
,my-add
必須接受的參數(shù)夕膀,此處我們并沒有要求必須參數(shù)個(gè)數(shù)虚倒,所以置空。 -
() #:rest (listof positive?)
产舞,此處就是對(duì)剩余參數(shù)的約束。 -
positive?
菠剩,返回值約束易猫。
我們同樣可以簡(jiǎn)單改寫:
(define/contract (my-add x . xs)
(->* (positive?) () #:rest (listof positive?) positive?)
(apply + (cons x xs)))
關(guān)鍵詞
關(guān)鍵詞的約束與普通函數(shù)寫法并不多,也用#:keyword
表示出來即可具壮。
(define/contract (my-add z #:x x #:y y)
(-> positive? #:x positive? #:y positive? positive?)
(+ x y z))
> (my-add 1 #:x 1 #:y 1)
3
> (my-add 0 #:x 1 #:y 1)
; my-add: contract violation
; expected: positive?
; given: 0
; in: the 1st argument of
; (->
; positive?
; #:x
; positive?
; #:y
; positive?
; positive?)
; contract from: (function my-add)
; blaming: top-level
; (assuming the contract is correct)
; at: stdin::1884-1890
> (my-add 1 #:x 0 #:y 1)
; my-add: contract violation
; expected: positive?
; given: 0
; in: the #:x argument of
; (->
; positive?
; #:x
; positive?
; #:y
; positive?
; positive?)
; contract from: (function my-add)
; blaming: top-level
; (assuming the contract is correct)
; at: stdin::1884-1890
>
默認(rèn)參數(shù)
默認(rèn)參數(shù)的約束跟不定參數(shù)一樣准颓,需要用到->*
。以我們?cè)凇澳J(rèn)參數(shù)”定義的my-add
為例棺妓。
(define/contract (my-add x [y 1] #:z [z 1])
(->* (positive?)
(positive?
#:z positive?)
positive?)
(+ x y z))
> (my-add 1)
3
> (my-add 0)
; my-add: contract violation
; expected: positive?
; given: 0
; in: the 1st argument of
; (->*
; (positive?)
; (positive? #:z positive?)
; positive?)
; contract from: (function my-add)
; blaming: top-level
; (assuming the contract is correct)
; at: stdin::2071-2077
> (my-add 1 0)
; my-add: contract violation
; expected: positive?
; given: 0
; in: the 2nd argument of
; (->*
; (positive?)
; (positive? #:z positive?)
; positive?)
; contract from: (function my-add)
; blaming: top-level
; (assuming the contract is correct)
; at: stdin::2071-2077
> (my-add 1 1 #:z 0)
; my-add: contract violation
; expected: positive?
; given: 0
; in: the #:z argument of
; (->*
; (positive?)
; (positive? #:z positive?)
; positive?)
; contract from: (function my-add)
; blaming: top-level
; (assuming the contract is correct)
; at: stdin::2071-2077
>