A function which will determine that if a passed in list follows an A B pattern

前端 未结 3 665
情深已故
情深已故 2021-01-26 19:34
(define fun4

 (lambda ( ls)

(cond ((null? ls ) #f)

 (cons (((eqv? \'a (car ls))) && ((eqv? \'b (cdr ls)))))

(else (pattern2 cdr ls)))))

In

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-26 20:32

    (define fun 
      (lambda (ls)
        (cond ((null? ls) #t)
              ((and (eq? (car ls) 'a)       ; the first item is a
                    (list? (cdr ls))        ; the rest of the list
                    (not (null? (cdr ls)))  ; which is not null
                    (eq? (cadr ls) 'b)      ; and it starts with b
                    (fun (cddr ls))) #t)    ; and the rest of the expression is 
              (else #f))))                  ; also in the A B format
    

    Running:

    > (fun '(a b a b))
    #t
    > (fun '(a b a))
    #f
    > (fun '(a b))
    #t
    > (fun '(a))
    #f
    > (fun '())
    #t
    > 
    

提交回复
热议问题