Hi there,
In the following code example,
(defun a-test (lst)
(let ((a (elt lst 0))
(b (elt lst 1))
(c (elt lst 2)))
(+ a (- c b))))
How to map local variable a, b, and c to the elements of lst? In my
actual code, the lst have 12 elements, and I have 12 named local
variables that appeared in the body with high frequency.
Hi there,
In the following code example,
(defun a-test (lst)
(let ((a (elt lst 0))
(b (elt lst 1))
(c (elt lst 2)))
(+ a (- c b))))
How to map local variable a, b, and c to the elements of lst? In my
actual code, the lst have 12 elements, and I have 12 named local
variables that appeared in the body with high frequency.
Best,
Jinsong
Hi there,
In the following code example,
(defun a-test (lst)
(let ((a (elt lst 0))
(b (elt lst 1))
(c (elt lst 2)))
(+ a (- c b))))
How to map local variable a, b, and c to the elements of lst? In my
actual code, the lst have 12 elements, and I have 12 named local
variables that appeared in the body with high frequency.
On Mon, 10 Apr 2023 21:05:23 +0800
Jinsong Zhao <[email protected]> wrote:
Hi there,
In the following code example,
(defun a-test (lst)
(let ((a (elt lst 0))
(b (elt lst 1))
(c (elt lst 2)))
(+ a (- c b))))
How to map local variable a, b, and c to the elements of lst? In my
actual code, the lst have 12 elements, and I have 12 named local
variables that appeared in the body with high frequency.
Why is the above code not satisfactory ?
Hi there,
In the following code example,
(defun a-test (lst)
(let ((a (elt lst 0))
(b (elt lst 1))
(c (elt lst 2)))
(+ a (- c b))))
How to map local variable a, b, and c to the elements of lst? In my
actual code, the lst have 12 elements, and I have 12 named local
variables that appeared in the body with high frequency.
In the following code example,
(defun a-test (lst)
(let ((a (elt lst 0))
(b (elt lst 1))
(c (elt lst 2)))
(+ a (- c b))))
How to map local variable a, b, and c to the elements of lst? In my actual code, the lst have 12 elements, and I have 12 named local variables that appeared in the body with high frequency.
Hi there,
In the following code example,
(defun a-test (lst)
(let ((a (elt lst 0))
(b (elt lst 1))
(c (elt lst 2)))
(+ a (- c b))))
How to map local variable a, b, and c to the elements of lst? In my
actual code, the lst have 12 elements, and I have 12 named local
variables that appeared in the body with high frequency.
Best,
Jinsong
Emacs macro seq-let can do the similar thing:
(seq-let (v1 v2) '(val1 val2 val3)
(message "%s %s" v1 v2))
I believe that common lisp must have a similar function, currently a
known way to do this is:
;; assign 5, 6 to v1, v2
(let (v1 v2)
(mapcar (lambda (sym val) (set sym val)) '(v1 v2) '(5 6)))
Emacs macro seq-let can do the similar thing:
(seq-let (v1 v2) '(val1 val2 val3)
(message "%s %s" v1 v2))
I believe that common lisp must have a similar function, currently a
known way to do this is:
;; assign 5, 6 to v1, v2
(let (v1 v2)
(mapcar (lambda (sym val) (set sym val)) '(v1 v2) '(5 6)))
On Monday, April 10, 2023 at 6:05:45 AM UTC-7, Jinsong Zhao wrote:
Hi there,
In the following code example,
(defun a-test (lst)
(let ((a (elt lst 0))
(b (elt lst 1))
(c (elt lst 2)))
(+ a (- c b))))
How to map local variable a, b, and c to the elements of lst? In my
actual code, the lst have 12 elements, and I have 12 named local
variables that appeared in the body with high frequency.
Best,
Jinsong
It isn't clear to us what behavior you want to have from your mapping.
In the code you show, each of the variables is bound to a particular element of your input list. So you can use them to reference the values of the list. But each of the variables is a local variable inside the LET form, so any change
to the variable is just a local effect. It will not change the value in the input
list unless the change you make on the local variable is a mutation of the object
that the variable is bound to.
The code you provided should return the value of (+ A (- C B)). What do you expect it to do?
Some other comments.
In general ELT is not a great function to use on lists, because it has to walk the
list until it gets to the Nth element. If you want to bind a number of variables to
elements of a list, then DESTRUCTURING-BIND will be more convenient and also more efficient.
But if you have a fixed size list where the individual elements have some meaningful
names, then perhaps an even better approach would be to use a struct (via DEFSTRUCT)
to provide named accessors to the elements. It would also have the side benefit
of being both more space and time efficient.
Thanks a lot for all your replies. DESTRUCTURING-BIND is what I want.
Before asking this question, I actually read its CLHS pages, however,
I did not understand it well.
On Wednesday, April 12, 2023 at 6:02:29 AM UTC-7, Jinsong Zhao wrote:
Thanks a lot for all your replies. DESTRUCTURING-BIND is what I want.
Out of curiosity, what is an example of the code that you want to write?
I ask because I wonder if a struct would be a better data structure than a list.
The list in my sample code is the size of the different sections decoded
from the binary header. Mapping them to locally named variables makes it
easy to check the validity of the file. Because it is only used once, structure is perhaps too heavy.
Also, there are rumors that structure is
obsolete and class should be used, which I am even less familiar with.
On Thu, 13 Apr 2023 18:14:39 +0800
Jinsong Zhao <[email protected]> wrote:
Also, there are rumors that structure is
obsolete and class should be used, which I am even less familiar with.
I don't know where you heard the "rumours" but they are wrong. Main differences between classes and structures :
Classes Structures
Support multiple
inheritance YES NO
Can be redefined YES NO ; undefined
behaviour if you attempt
to do so.
So classes offer greater flexibility at the cost of execution speed
although I don't think it would cost too much in execution speed
if structures also offered multiple inheritance.
With structures you also get automatically defined accessors , print methods and copier functions ; you can optionally define the layout to be a vector or a list but then the structure does not become a new type in the CL type system but counts as a vector or list respectively.
Jinsong Zhao <[email protected]> writes:
Before asking this question, I actually read its CLHS pages, however,
I did not understand it well.
It just takes time to familiarize yourself with it.
Be sure to check out the Common Lisp Cookbook as well, it's often a bit easier to follow at least initially.
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (3 / 13) |
| Uptime: | 142:25:30 |
| Calls: | 12,089 |
| Calls today: | 2 |
| Files: | 14,998 |
| Messages: | 6,517,451 |