2009-06-27 42 views
3

如何接受以下輸入?方案中的模式匹配

(list of 0 or more charcters and ends with 3) or 
(list of 1 or more characters 4 and 0 or more characters after 4) 

(match (list 3)) -> #t 
(match (list 1 2 3)) -> #t 
(match (list 1 2 3 4)) -> #t 
(match (list 1 2 3 4 5)) -> #t 
(match (list 4)) -> #f 

編輯:這不是我的功課。我試圖從PAIP寫ELIZA之類的東西,但我只知道如何編寫一個以單詞開始的模式。

回答

3

你提到的字符,但在你的例子中使用數字。我在這裏使用數字,但切換到字符是微不足道的。

(require scheme/match) 
(define satisfies 
    (match-lambda 
    [(list (? number?) ... 3) #t] 
    [(list (? number?) ..1 4 (? number?) ...) #t] 
    [_ #f]))