lisp

Circular list in Common Lisp

走远了吗. 提交于 2019-11-28 13:31:28
I am working using a visual programming environment for musical composition based on CL . I am trying to create a function that when given say 3 elements (1 2 3) will return 1, 2, 3, 1, 2, 3 etc., one number at the time each time it is evaluated. The book Common Lisp a Gentle Introduction , mentions briefly that it's possible to create circular lists using sharp-equal notation but does not get into details on how to use them. Keep in mind that I can insert actual Lisp code in the program using a object specifically designed for that. CL-USER 3 > (defun circular (items) (setf (cdr (last items))

operator #+ and #- in .sbclrc

久未见 提交于 2019-11-28 13:26:22
Anybody know what #+ and #- operators means in .sbclrc ? I couldn't find it in the manual. I see #- in .sbclrc after I installed quicklisp: #-quicklisp (let ((quicklisp-init (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))) (when (probe-file quicklisp-init) (load quicklisp-init))) I also see #+ in the SBCL User Manual, but I couldn't find explanation of their functionality. Looks like something related for loading individual module. Are they only for SBCL implementation or part of Common lisp? That's a general facility of Common Lisp, not only SBCL. There is a variable cl:

Replacing a symbol in a symbolic expression

别来无恙 提交于 2019-11-28 12:32:55
问题 I wish to replace the first occurrence of a symbol within pairs. For example: take (define n '((a . b) . (a . d))) and i define a method context to replace the first instance (left most) of X with '() replacing a should give me: ((() . b) a . d) however i am stuck as my method replaces ALL instances and i am not sure how to add a check for this. my code is as follows: (define (context s sym) (cond ((null? s) #f) ((atom? s) (if (equal? s sym) '() s )) (else (cons (context (car s) sym) (context

Reading a character without requiring the Enter button pressed

痞子三分冷 提交于 2019-11-28 12:27:15
read-line and read-char both require you press Enter key after typing something. Is there any mechanism in Common Lisp that would allow the program to continue upon the press of any single character immediately, without requiring the additional step of pressing Enter? I'm trying to build a quick, dynamic text input interface for a program so users can quickly navigate around and do different things by pressing numbers or letters corresponding to onscreen menus. All the extra presses of the Enter key seriously interrupt the workflow. This would also be similar to a "y/n" type of interrogation

Replace elements in nested quoted lists adds new elements?

烂漫一生 提交于 2019-11-28 12:24:44
问题 I have a nested list, and I am trying to non-destructively replace all its elements (inside the nested list as well). That is, given my input list '(1 '(2 3 4) '(5 6 7) 8 9) I am trying to achieve '(0 '(0 0 0) '(0 0 0) 0 0) I tried the following (defun subs-list (list value) "Replaces all elements of a list of list with given value" (loop for elt in list collect (if (listp elt) (subs-list elt value) value))) But when I try (subs-list '(1 '(2 3 4) '(5 6 7) 8 9) 0) (0 (0 (0 0 0)) (0 (0 0 0)) 0

How can I optimise my recursive Lisp function

帅比萌擦擦* 提交于 2019-11-28 11:27:04
问题 I am trying to create a function prime-factors that returns the prime factors of a number. To do so, I created is-prime function, and prime-factors-helper that will do a recursive check of the prime factors. (defun is-prime (n &optional (d (- n 1))) (if (/= n 1) (or (= d 1) (and (/= (rem n d) 0) (is-prime n (- d 1)))) ())) (defun prime-factors-helper (x n) (if (is-prime x) (list x) (if (is-prime n) (if (AND (= (mod x n) 0) (<= n (/ x 2))) (cons n (prime-factors-helper (/ x n) n)) (prime

Is it possible something like lvalue of perl or setf of lisp in python?

主宰稳场 提交于 2019-11-28 10:50:46
问题 In lisp you can say: (setf (aref a 1) 5) In perl you can say: substr( $string, $start, $stop ) =~ s/a/b/g Is it possible something like this in python? I mean is it possible to use function result as a lvalue (as a target for assignment operation)? 回答1: No. Assigning to the result of a function call is specifically prohibited at the compiler level: >>> foo() = 3 File "<stdin>", line 1 SyntaxError: can't assign to function call There are however two special cases in the Python syntax: # Slice

lisp filter out results from list not matching predicate

↘锁芯ラ 提交于 2019-11-28 10:40:13
I am trying to learn lisp, using emacs dialect and I have a question. let us say list has some members, for which predicate evaluates to false. how do I create a new list without those members? something like { A in L: p(A) is true } . in python there is filter function, is there something equivalent in lisp? if not, how do I do it? Thanks rootzlevel These functions are in the CL package, you will need to (require 'cl) to use them: (remove-if-not #'evenp '(1 2 3 4 5)) This will return a new list with all even numbers from the argument. Also look up delete-if-not , which does the same, but

LISP - Global variable keep their old value after reinitialization

醉酒当歌 提交于 2019-11-28 10:38:09
问题 I am creating a expert system with Common Lisp for my study. There is a global variable : BF -> fact base. I initialize like that : (defvar *BF* NIL) My "main function" call to the function "initialize" which set the global variable with big data. (defun initialize () (setf *BF* '( (metric ( (CPU-utilization NIL) (RPI NIL) (ART NIL) (concurent-invocation NIL) (stall-count NIL) (GC-bytes-in-use NIL) (available-thread NIL) (available-connection NIL) (instance-count NIL) )) (problem ( (livelock

Transposing lists in Common Lisp

喜你入骨 提交于 2019-11-28 10:01:52
I am trying to transpose a list of lists; my comments indicate the thought process. (setq thingie '((1 2 3) (4 5 6) (7 8 9))) ;;test case (defun trans (mat) (if (car mat) (let ((top (mapcar 'car mat)) ;;slice the first row off as a list (bottom (mapcar 'cdr mat))) ;;take the rest of the rows (cons top (trans bottom)))) ;;cons the first-row-list with the next-row-list mat) (trans thingie) => ((1 2 3) (4 5 6) (7 8 9)) ;;wait what? But, I really want it to be ((1 4 7) (2 5 8) (3 6 9)) What am I doing wrong? There is a simple way for this: (defun rotate (list-of-lists) (apply #'mapcar #'list list