I am making a pure Lisp interpreter, and trying to write a reader to convert lists to cons pairs.
From what I've read lists are internally cons pairs such as this:
( 1 2 3 ) = (1.(2.(3.NIL)))
but I have no clue how to implement a nested list such as the following with cons pairs
( (1 2) (3 4 ) (5 6 ) )
How is this supposed to look like unabbreviated?
CL-USER 40 > (sdraw::sdraw '( (1 2) (3 4 ) (5 6 ) ))
[*|*]------------------>[*|*]------------------>[*|*]--->NIL
| | |
v v v
[*|*]--->[*|*]--->NIL [*|*]--->[*|*]--->NIL [*|*]--->[*|*]--->NIL
| | | | | |
v v v v v v
1 2 3 4 5 6
That would be
> '((1 . (2 . ())) . ((3 . (4 . ())) . ((5 . (6 . ())) . ())))
'((1 2) (3 4) (5 6))
or
? '((1 . (2 . nil)) . ((3 . (4 . nil)) . ((5 . (6 . nil)) . nil)))
((1 2) (3 4) (5 6))
See this question for a Scheme program (which is trivial to translate to Common Lisp) that prints a list as a dotted pair.
Related
How would I write a function in Dr. Racket which consumes a list of list of integers and produces a new list of list of integers with a transformation (sqr) applied to each element.
Note: There is an equal number of elements in each list.
Here are two examples to show what I am saying:
Example 1:
(list (list 1 2) (list 3 -2))
Should produce:
(list (list 1 4) (list 9 4))
Example 2:
(list (list 3 4 5) (list 2 1 3) (list 2 3 7) (list 0 -3 2))
Should produce:
(list (list 9 16 25) (list 4 1 9) (list 4 9 49) (list 0 9 4))
Here is my code so far:
(define (transform-to-sqr b)
(map sqr b))
If I type in:
(transform-to-sqr (list (list 1 2) (list 3 -2)))
I get an error message saying:
sqr: expected a number; given (list 1 2).
Why is this happening. Can someone please tell me what I am doing wrong.
Thanks for your help.
The map higher-order procedure applies a procedure over the elements of a list, but in this case the elements are lists themselves. To operate on the nested elements, we need to nest calls to map:
(define (transform-to-sqr matrix)
(map (λ (row) (map sqr row))
matrix))
It works as expected:
(transform-to-sqr '((1 2) (3 -2)))
=> '((1 4) (9 4))
(transform-to-sqr '((3 4 5) (2 1 3) (2 3 7) (0 -3 2)))
=> '((9 16 25) (4 1 9) (4 9 49) (0 9 4))
This question already has an answer here:
Nested Loops Using Loop Macro in Common Lisp
(1 answer)
Closed 2 years ago.
I want to create a list of pairs (a . b) with 1 < a < b <= n up to n, e.g. n = 5:
((2 . 3) (2 . 4) (3 . 4) (2 . 5) (3 . 5) (4 . 5))
(The order of the pairs is not critical.)
I came up with the code
(defun create-pairs (upper-bound)
(loop for i from 3 to upper-bound
for j from 2 to (1- upper-bound)
collecting (cons j i)))
but that doesn't do what I wanted
* (create-pairs 5)
((2 . 3) (3 . 4) (4 . 5))
since the loops are incrementing at the same time.
Therefore I tried this
(defun create-pairs (upper-bound)
(loop for i from 3 to upper-bound do
(loop for j from 2 to (1- upper-bound)
collecting (cons j i))))
with the result:
* (create-pairs 5)
NIL
I don't remember where but I read that it's not possible to use collecting in constructs like my second try.
So how do I get the result I want to have? Isn't it possible to solve this with loop for?
You are almost there - you just need to accumulate the results of the inner loop:
(defun create-pairs (upper-bound)
(loop for i from 3 to upper-bound nconc
(loop for j from 2 below i
collect (cons j i))))
(create-pairs 5)
==> ((2 . 3) (2 . 4) (3 . 4) (2 . 5) (3 . 5) (4 . 5))
I have a function that return two splited list like below :
((1 . 2) (3 . 4) (5 . 7))
((8 . 9) (10 . 23) (30 . 20))
Is there any resource in common lisp to do like python
a,b = 1,2
a = 1
b = 2
There are two options. First, you can return multiple values with VALUES. You can then use MULTIPLE-VALUE-BIND to bind the return values to different variables.
(defun foo ()
(values '((1 . 2) (3 . 4) (5 . 7))
'((8 . 9) (10 . 23) (30 . 20))))
(multiple-value-bind (a b) (foo)
(format t "~&A: ~s~%B: ~s~%" a b))
; A: ((1 . 2) (3 . 4) (5 . 7))
; B: ((8 . 9) (10 . 23) (30 . 20))
This is a little different from Python, because you can call the function as if it only returned one value, and the other values will be silently discarded. Multiple return values are usually used when the first value makes sense on its own, and the others are just supplementary information (see for example FLOOR).
In this case it seems like the two values are related to each other (so that it never makes sense to use only the first value). So in this case it's probably better to return a list, or a cons-cell, instead. You can use DESTRUCTURING-BIND to assign the elements to variables.
(defun bar ()
(list '((1 . 2) (3 . 4) (5 . 7))
'((8 . 9) (10 . 23) (30 . 20))))
(destructuring-bind (a b) (bar)
(format t "~&A: ~s~%B: ~s~%" a b))
; A: ((1 . 2) (3 . 4) (5 . 7))
; B: ((8 . 9) (10 . 23) (30 . 20))
If you already have two variables a and b, you can assign the values:
CL-USER 6 > (let ((a 10)
(b 3))
(multiple-value-setq (a b) (truncate a b))
(list a b))
(3 1)
or alternatively using SETF:
CL-USER 7 > (let ((a 10)
(b 3))
(setf (values a b) (truncate a b))
(list a b))
(3 1)
#lang racket
I need to create a pair from an element and a list
however when I do (cons 2 (list 1 2 3)) I get (2 (1 2 3)), I want to get (2 . (1 2 3))
how do I get the dot in ?
Since (cons a b) is the same as (a . b) we get that (2 . (1 2 3)) is the same as (cons 2 (list 1 2 3) which is the same as (list 2 1 2 3). To confirm:
> (cons 2 (list 1 2 3))
'(2 1 2 3)
> '(2 . (1 2 3))
'(2 1 2 3)
Note that the printer prints (2 1 2 3) and not (2 . (1 2 3)). The printer attempts to use the dot only when absolutely needed.
There is a difference between the syntax that explains the structure, how it's stored and how display would represent it.
For a list of two elements there are two ways you can represent it (1) and (1 . ()). When this list is displayed it will always prefer the one with the least parentheses. Thus '(2 . (1 2 3)) will always be printed as (2 1 2 3) by display. If you don't want that you can make yourself a cons-write like:
;; displays cons always as dotted
(define (cons-write x)
(if (pair? x)
(begin
(display "(")
(cons-write (car x))
(display " . ") ; spaces are important
(cons-write (cdr x))
(display ")"))
(write x)))
(cons-write '(1 2 3 4)) ; prints (1 . (2 . (3 . (4 . ()))))
A function that receives a list with sublist's M*N and returns the sum of all elements of the sublist's
example: (solution '( (1 2 3) (4 5 6) ) )
return: (5 7 9)
Sry for the bad english
thks =)
A Common Lisp version is almost the same as in Scheme:
(defun solution (list)
(apply #'mapcar #'+ list))
(solution '((1 2 3) (9 10 11) (3 4 5)))
; ==> (13 16 19)
It depends on what Lisp interpreter you're using. In Scheme, this will work:
(define (solution lsts)
(apply map + lsts))
For example:
(solution '((1 2 3) (4 5 6)))
=> '(5 7 9)