lisp

Is there a way to extract all elements of a list in place

时间秒杀一切 提交于 2019-12-08 20:38:24
问题 Im looking for a way to extract all the elements of a list in common lisp. Like this [194]> (break-out-of-list '(a b c d)) A B C D Edit: The usage example I gave was not thought out very well, however I'm still curious if it is possible to break out of a list like in the example above. 回答1: What you demonstrate seems to be the question how to get the elements of a list as multiple values : CL-USER> (values 1 2 3) 1 2 3 CL-USER> (apply #'values '(1 2 3)) 1 2 3 See also multiple-value-bind and

How to calculate difference between two sets in emacs lisp,the sets should be lists

故事扮演 提交于 2019-12-08 19:40:11
问题 How to calculate the difference between two sets in Emacs Lisp? The sets should be lists. The programm should be very simple and short, or else I won't understand it. I'm a newbee. Thx 回答1: There is a set-difference function in the Common Lisp extensions: elisp> (require 'cl) cl elisp> (set-difference '(1 2 3) '(2 3 4)) (1) 回答2: When I write Elisp code that has lots of list data transformations, I use dash library, because it has loads of functions to work with lists. Set difference can be

Is there a common LISP function to compare the contents of two lists?

送分小仙女□ 提交于 2019-12-08 19:23:25
问题 In particular, I just want to ensure that two lists have the same elements, ignoring order 回答1: According to Steele "set-difference returns a list of elements of list1 that do not appear in list2. This operation is not destructive." So if the set-difference is empty and the lengths are the same... http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node152.html#SECTION001950000000000000000 回答2: If order isn't important, you can use equal. For instance, (equal (list 1 2) (list 1 2)) is true. Thus

No-argument (and) returns t

我怕爱的太早我们不能终老 提交于 2019-12-08 19:16:09
问题 Both CL and Scheme define (and) to return t (or #t ) with no arguments. I'm trying to understand the rationale for this. My naive assumption is that an empty set is false, and passing in zero arguments feels like passing in nothing that can be true. Edit: clojure follows the same convention. I must be missing some basic Lisp assumption. 回答1: The empty product is 1. The reason is that 1 is a neutral element for * . If you have the product of 2 and 3 and then multiply by the product of nothing,

How do collector functions work in Scheme?

末鹿安然 提交于 2019-12-08 17:48:07
问题 I am having trouble understanding the use of collector functions in Scheme. I am using the book "The Little Schemer" (by Daniel P. Friedman and Matthias Felleisen). A comprehensive example with some explanation would help me massively. An example of a function using a collector function is the following snippet: (define identity (lambda (l col) (cond ((null? l) (col '())) (else (identity (cdr l) (lambda (newl) (col (cons (car l) newl)))))))) ... with an example call being (identity '(a b c)

Appending to the result of a “loop-collect” in Lisp

好久不见. 提交于 2019-12-08 15:53:22
问题 Let's say I run the following (loop for i to 4 collect i) Then I get a list (0 1 2 3 4) . Now, if I want to append something to the result, I may use rplacd on its last element, but since Lisp lists are linked lists, it's not very efficient. Here the list is ridiculously small, but it's only an example. However, since the loop facility returns the list in increasing order, it has to keep track of a pointer to the last element, and update the result with rplacd , or something equivalent. A

Breadth first search in LISP

亡梦爱人 提交于 2019-12-08 14:08:48
问题 I have a representation of a tree using lists. For example: (1 ((2 (3)) (3 (2)))) (2 ((1 (3)) (3 (1)))) (3 ((1 (2)) (2 (1)))))` Now I need to traverse it level by level while maintaining the hierarchy tree. For instance: Traversing root node (1) Traversing depth 1 (1 2) (1 3) (2 1) (3 1) (3 1) (3 2) Traversing depth 2 (1 2 3) (1 3 2) (2 1 3) (2 3 1) (3 1 2) (3 2 1) I can't figure out how to do it in Lisp. Any help (even a pseudo code) is appreciated. I have thought of several approaches but

Adding two lists

会有一股神秘感。 提交于 2019-12-08 12:38:43
问题 I want to add two lists : (1 2 3) and (5 3 4) should yield (6 5 7). The function should add the elements on the corresponding position, so even if I would have (9 1 2) + ( 5 2 6) , it should yield (14 3 8). My function (defun add(l r) (setf return-value '()) (loop for i from 0 to (- (length l) 1) do (setf return-value (cons (+(nth i l)(nth i r)) return-value)) ) (reverse return-value) ) How could I create a simmilar function which would subtract the lists ? 回答1: Polynomial arithmetic This

Can be this lisp function be implemented recursively?

坚强是说给别人听的谎言 提交于 2019-12-08 11:44:39
问题 The goal of this function is to generate the cartesian product of 2 list. For example (combo (list 1 2 3) (list 4 5)) => (1 4) (1 5) (2 4) (2 5) (3 4) (3 5) (defun combo (l1 l2) (let ( (res (list)) ) (dolist (elem1 l1 res) (dolist (elem2 l2 res) (setq res (append res (list(list elem1 elem2)))) ) ) ) ) How could be implemented recursively? 回答1: A simple recursive implementation would be to use two helper functions; one to traverse through L1 ( %COMBO in the code below), calling another

LISP dynamically define functions

妖精的绣舞 提交于 2019-12-08 07:22:47
问题 I want to define a function with a parameter that defines another function with that parameter as the name. example that dont work: (DEFUN custom (name op const var) (DEFUN name (var) (op const var))) The problem is that name isnt evaluated and so the function that is defined is always called name. I know that FUNCALL and APPLY can evaluate parameter dynamically, but i don't know how to call FUNCALL DEFUN... correctly. 回答1: I don't think you can use nested defuns. You can either use a defun