Add elements from a list to associative list in correct format in lisp

154 Views Asked by At

let list1 equal '((1 2) (3 4))

also let env be nil

why does (acons (car (car list1)) (cdr (car list1)) env) return ((1 2))

while (acons 1 2 env) return ((1 . 2))

I need the first line to also return ((1 . 2))

1

There are 1 best solutions below

1
Martin Půda On

See the definition of acons:

Creates a fresh cons, the cdr of which is alist and the car of which is another fresh cons, the car of which is key and the cdr of which is datum.

If list1 is result of (list (list 1 2) (list 3 4)), then:

  • (car (car list1)) is 1
  • (cdr (car list1)) is (2)

And (cons 1 (list 2)) is (1 2).

You can use first and second:

=> (acons (first (car list1)) (second (car list1)) env)
((1 . 2))