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?
Asked
Active
Viewed 98 times
0
-
1[This answer](http://stackoverflow.com/a/16379759/1281433) to [Recursive range in Lisp adds a period?](http://stackoverflow.com/q/16379657/1281433) includes an explanation of how pairs are used to implement lists in Scheme (and other Lisps). It's not really clear what you're asking here, though. A list is either the empty list `()`, or a pair whose `car` is the first element of the list and whose `cdr` is the rest of the list (another list). – Joshua Taylor Nov 11 '13 at 00:54
-
what about your [previous question](http://stackoverflow.com/questions/19886294/issues-with-conditionals-in-scheme)? Have you finished with it? If so, you can accept one of the answers to signal this to the other participants on this site. :) – Will Ness Nov 11 '13 at 01:33
1 Answers
2
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)))))

Sylwester
- 47,942
- 4
- 47
- 79
-
2It shouldn't. A `cond` returning `#t` or `#f` is in indication that logical connectives are in order. – GoZoner Nov 11 '13 at 14:58