Building a List in Lisp: Novice's Mess Solved

In summary, the conversation discusses the difficulties faced by a lisp novice in trying to return a list from a function with arguments x and y. The first attempt using the list function did not evaluate x and y, while the second attempt resulted in an unbound datum error. A solution was found by using list functions to create the desired list.
  • #1
Mentz114
5,432
292
I'm a lisp novice, so naturally I've got into a mess trying to do something that would be elementary in a procedural language. I want to return this list

( ( a SIMP) ( (MLIST SIMP) x ) (MLIST SIMP) y ) )

from a func where x,y are args passed to the function. x and y are atoms.

This
(list '( a SIMP) '( (MLIST SIMP) x ) '(MLIST SIMP) y ) )
does not fail but obviously x and y are unevaluated.

This
(list '( a SIMP) ( ('MLIST 'SIMP) x ) ('MLIST 'SIMP) y ) )
complains about an unbound datum.

I tried to delete this thread but I couldn't. A solution is

(setq lo (list '(MLIST SIMP) x))
(setq hi (list '(MLIST SIMP) y))
(setq res (list '(a SIMP) lo hi ))
 
Last edited:
Technology news on Phys.org
  • #2
This works, but I'm sure there must be a more elegant way.You can use list functions to get the desired result:(list '(a SIMP) (list (list 'MLIST 'SIMP) x) (list 'MLIST 'SIMP) y)
 

Related to Building a List in Lisp: Novice's Mess Solved

1. What is Lisp and why is it used for building lists?

Lisp is a programming language known for its powerful list processing abilities and expressive syntax. It is used for building lists because of its built-in list data structure and numerous list manipulation functions, making it an ideal choice for managing and manipulating data in a structured way.

2. How do I create a list in Lisp?

To create a list in Lisp, you can use the cons function which takes two arguments, the first being the element to add to the list and the second being the existing list. For example, (cons 1 '(2 3)) will create a list (1 2 3).

3. How do I add or remove elements from a list in Lisp?

To add or remove elements from a list in Lisp, you can use functions such as append, push, pop and nconc. These functions allow you to add or remove elements from a list, either at the beginning, end or at a specific position.

4. How do I access elements within a list in Lisp?

To access elements within a list in Lisp, you can use the car and cdr functions. car returns the first element of the list while cdr returns the rest of the list. You can also use the nth function to access elements at a specific index within the list.

5. How do I check if a list is empty in Lisp?

To check if a list is empty in Lisp, you can use the null function which returns t if the list is empty and nil if it is not. You can also use the length function to check the length of the list and see if it is equal to 0.

Similar threads

  • Computing and Technology
Replies
4
Views
13K
  • Programming and Computer Science
Replies
32
Views
8K
Back
Top