0
votes

Is there any way to make or add to a list in Scheme with only these functions available: equal?, car, cdr, cons, cond, if, quote, +, *, null?, symbol? , integer?, any car/cdr variant, define, let. (You'll notice that the use of any #list or #append calls is not allowed).

I've tried cons several different ways, but it just gives things like '(0 . 10) rather than just (0 10) which would be ideal. Yes this is part of a larger assignment.

2

2 Answers

1
votes

In Scheme, a "proper list" ends with the empty list. So, you can make '(0 10) like this:

(cons 0 (cons 10 '()))

In general, you can always add something to the front of a proper list with (cons new-element old-list), and you'll get a new proper list. The cdr of a non-null proper list is always itself a proper list. The car can be anything.

The word cons is short for "construct": it's the most primitive procedure for constructing lists.

0
votes

In general: You can define ALL built in procedures, which are NOT "special forms", for your own.

Define your own

(define (append-my List1 List2)
   (if (null? List1)
       list2
       (cons (car List1) 
             (append-my (cdr List1) List2))))

In the book "Structure and Interpretation of Computer Programs" you find many definitions for built-in procedures. Every body interested in Scheme or Lisp should study the first three chapters of SICP.