スキームにおけるパターンマッチング
-
20-08-2019 - |
質問
どのようにして、次の入力を受け付けていますか?
(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ような何かを書き込もうが、私は言葉で始まるパターンを書くことだけ方法を知っています。
解決
あなたは文字を言及したが、その後、あなたの例では数字を使用しています。私はここの数字を使用しますが、文字への切り替えは簡単ですよ。
(require scheme/match)
(define satisfies
(match-lambda
[(list (? number?) ... 3) #t]
[(list (? number?) ..1 4 (? number?) ...) #t]
[_ #f]))
所属していません StackOverflow