lisp

Formatting a list

混江龙づ霸主 提交于 2019-12-11 00:46:39
问题 I have the following list: (X X O NIL NIL O NIL NIL O) I'd like to format it to look like this: X | X | O --+---+-- | | O --+---+-- | | O I could probably cobble something together with what little I know about Lisp and FORMAT , but it would probably be pretty gross. Any pointers would be greatly appreciated. 回答1: * (format t "~{~A | ~A | ~A~%~^--+---+--~%~}" (mapcar (lambda (x) (or x " ")) '(X O X NIL X X O X NIL))) X | O | X --+---+-- | X | X --+---+-- O | X | NIL 回答2: * (format t "~{~A |

CLISP - Reversing a simple list

孤街浪徒 提交于 2019-12-10 23:49:28
问题 I have to reverse the elements of a simple (single-dimension) list. I know there's a built-in reverse function but I can't use it for this. Here's my attempt: (defun LISTREVERSE (LISTR) (cond ((< (length LISTR) 2) LISTR) ; listr is 1 atom or smaller (t (cons (LISTREVERSE (cdr LISTR)) (car LISTR))) ; move first to the end ) ) Output pretty close, but is wrong. [88]> (LISTREVERSE '(0 1 2 3)) ((((3) . 2) . 1) . 0) So I tried to use append instead of cons : (t (append (LISTREVERSE (cdr LISTR))

Macro calling a function works in interpreter, fails in compiler (SBCL + CMUCL)

天涯浪子 提交于 2019-12-10 22:53:23
问题 As suggested in a macro-related question I recently posted to SO, I coded a macro called "fast" via a call to a function (here is the standalone code in pastebin): (defun main () (progn (format t "~A~%" (+ 1 2 (* 3 4) (+ 5 (- 8 6)))) (format t "~A~%" (fast (+ 1 2 (* 3 4) (+ 5 (- 8 6))))))) This works in the REPL, under both SBCL and CMUCL: $ sbcl This is SBCL 1.0.52, an implementation of ANSI Common Lisp. ... * (load "bug.cl") 22 22 $ Unfortunately, however, the code no longer compiles: $

Lisp symbols without package bindings

人盡茶涼 提交于 2019-12-10 22:24:07
问题 I've been working on some project. It should be able to do numerical and symbolic computing. But now I stuck on one problem and I don't really know how to resolve it. To be specific and short, let's say we are in package (in-package #:brand-new-package) Where we have symbol database (defvar var-symbol-database (make-hash-table :test #'equal)) Reading and setting functions (defun var-symbol (name) (get-hash name var-symbol-database)) (defun set-var-symbol (name value) (setf (get-hash name var

Is this an implementation-specific behavior for literal cons?

拟墨画扇 提交于 2019-12-10 21:38:28
问题 I'm testing out the code in this interesting answer. CL-USER> (defun literal-cons () (let ((cons '(1 . 2))) (incf (cdr cons)) cons)) ; in: DEFUN LITERAL-CONS ; (INCF (CDR CONS)) ; --> LET* ; ==> ; (SB-KERNEL:%RPLACD #:CONS1 #:NEW0) ; ; caught WARNING: ; Destructive function SB-KERNEL:%RPLACD called on constant data. ; See also: ; The ANSI Standard, Special Operator QUOTE ; The ANSI Standard, Section 3.2.2.3 ; ; compilation unit finished ; caught 1 WARNING condition LITERAL-CONS CL-USER>

Forming Lisp code to task — related to flatten list method

放肆的年华 提交于 2019-12-10 21:34:05
问题 I'm having issues trying to form code for a problem I want to resolve. It goes like this: ~ Goal: flatten a nested list into one number If the object is a list, replace the list with the sum of its atoms. With nested lists, flatten the innermost lists first and work from there. Example: (CONDENSE '(2 3 4 (3 1 1 1) (2 3 (1 2)) 5)) (2 3 4 (6) (2 3 (3)) 5) (2 3 4 (6) (8) 5) (28) => 28 I've tried to implement the flatten list function for this problem and I ended up with this: (defun condense

SBCL Run Shell Command

♀尐吖头ヾ 提交于 2019-12-10 20:43:32
问题 I've seen Executing a shell command from Common Lisp and its answers, but I'm still not sure whether SBCL provides a way execute shell commands from code. The SBCL Manual does support POSIX, but I was hoping for something a bit more higher level. Specifically, I want to call a Python script and capture the return value. Is there any way to do this? 回答1: Given the file test.py : import sys sys.exit(42) You can run it with sb-ext:run-program and examine the exit code as follows: CL-USER> (sb

Definition of tree structure in Lisp

我怕爱的太早我们不能终老 提交于 2019-12-10 20:11:49
问题 From the Common Lisp HyperSpec glossary: tree n. 1. a binary recursive data structure made up of conses and atoms: the conses are themselves also trees (sometimes called "subtrees" or "branches"), and the atoms are terminal nodes (sometimes called leaves). Typically, the leaves represent data while the branches establish some relationship among that data. 2. in general, any recursive data structure that has some notion of "branches" and leaves. tree structure n. (of a tree) the set of conses

Merge 2 sorted lists

风流意气都作罢 提交于 2019-12-10 20:00:44
问题 I've been asked to come up with as many solutions as possible to the following problem: Write a function which takes two lists of numbers (both assumed to be in ascending order) and merges them into a single list (also in ascending order). My first solutions was to append list1 onto list2 and then re- sort . Then I found a built-in merge . Then I decided to actually implement a solution myself, and I came up with a tail recursive function that, at the moment, only works for a subset of lists.

Spread a list into parent sexp

故事扮演 提交于 2019-12-10 19:45:24
问题 Is there a form in any lisp that could "spread" a list in the parent sexp? Like: (+ (spread '(1 2 3))) -> (+ 1 2 3) 回答1: There are two way to do it. Which is better depends on what you want in the end. Generally, you can use ,@ inside ` (backquote). The form following ,@ is evaluated to produce a list, which is then spliced into the template: * `(+ ,@'(1 2 3)) (+ 1 2 3) * (eval `(+ ,@'(1 2 3))) 6 Or, if you just want to call a function with its arguments which are packed in a list, it will be