Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an empty list in Racket

Tags:

list

null

racket

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?

like image 740
Davrand Avatar asked Dec 23 '10 18:12

Davrand


People also ask

What is an empty list in Racket?

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.

How do you create an empty list in Scheme?

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.

What does list do in Racket?

lst : list? Returns the number of elements in lst. This function takes time proportional to that length.

How do you find the smallest number in a list racket?

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.


2 Answers

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)
like image 53
Sean Devlin Avatar answered Nov 12 '22 18:11

Sean Devlin


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 '())
like image 41
Chris Jester-Young Avatar answered Nov 12 '22 19:11

Chris Jester-Young