I'm teaching myself LISP with online text of Structure and Interpretation of Computer Programs, but it differs in small details with the Racket program I'm running to learn LISP on. For example, SICP says that the terminating element of any list is 'nil', but Racket doesn't support 'nil'. How do I create an empty list in Racket so I can test my own procedures?
The list is the fundamental data structure of Racket. A list is a sequence of values. A list can contain any kind of value, including other lists. A list can contain any number of values. If a list has zero values, it's called the empty list.
You can write the empty list as a literal in your programs as '() . That is, the expression '() returns the empty list (null pointer), () . Later I'll explain why you have to put the single quote mark in front of the empty set of parentheses when writing the empty list as a literal.
lst : list? Returns the number of elements in lst. This function takes time proportional to that length.
You default case can be as follows: You use find-smallest to find the smallest of the rest of the list and compare this to the first element, eg. with < . The smallest should then be the result.
The empty list is denoted '()
. So you can create a list like
(cons 1 (cons 2 (cons 3 '())))
This produces the list
'(1 2 3)
Sean's answer is correct. However, if you want to be able to type nil
, then that's easy too. Just run this once at the start of your session:
(define nil '())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With