(define fun4
(lambda ( ls)
(cond ((null? ls ) #f)
(cons (((eqv? \'a (car ls))) && ((eqv? \'b (cdr ls)))))
(else (pattern2 cdr ls)))))
In
There are many, many errors in your solution. Let's see what's wrong in each of the conditions:
#f immediately, and notice how we use cadr for accessing the second element, because && doesn't work in Scheme, you must use and for the logical and operation. Also you have unnecessary, erroneous parentheses surrounding each test (by the way: those were the ones causing the "expected procedure" error)cddr. Also you must call fun4 to advance the recursion, not pattern2This is the correct way to solve the problem, notice how the above issues were addressed:
(define fun4
(lambda (ls)
(cond ((null? ls) #t) ; 1
((null? (cdr ls)) #f) ; 2
((not (and (eq? 'a (car ls)) (eq? 'b (cadr ls)))) #f) ; 3
(else (fun4 (cddr ls)))))) ; 4
Always test your procedures, the above will work correctly:
(fun4 '())
=> #t
(fun4 '(a))
=> #f
(fun4 '(a b))
=> #t
(fun4 '(a b a))
=> #f
(fun4 '(a b a b))
=> #t
As a final note, if the empty list is not supposed to follow the pattern, then check for it before calling fun4 and return #f if the initial input list is empty.
(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
>
So much wheel reinvention. Just use SRFI 1!
(require srfi/1)
(define (fun4 lst)
(every eq? lst (circular-list 'a 'b)))
(This operates under the assumption that (a b a) should be valid rather than invalid.)