문제

다음 입력을 어떻게 받아들이나요?

(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]))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top