Oren Shoemaker
Oren Shoemaker

Reputation: 19

Need help understanding lists in Scheme

I'm having trouble understanding how Scheme forms and then detects lists. Hows does it determine the difference between a list and a dotted pair?

Upvotes: 0

Views: 108

Answers (1)

Sylwester
Sylwester

Reputation: 48745

A list structure is formed from pairs. A proper list is a chain of pairs where the last pair has the empty list as its cdr.. We can make list? like this:

(define (list? lst)
  (cond ((null? lst) #t)
        ((pair? lst) (list? (cdr lst)))
        (else #f)))

Or you could use and/or:

(define (list? lst)
  (or (null? lst)
      (and (pair? lst)
           (list? (cdr lst)))))

Upvotes: 2

Related Questions