A single quote followed by the written representation of a value will produce that value: Example: '(1 x "foo") will produce a value that prints as (1 x "foo"). Suppose now that I don't want a literal symbol x in the list. I have a variable x in my program,...
design-patterns,coding-style,common-lisp,anti-patterns,dynamic-scope
Actually you can bind *standard-output* directly: (defun test-im-vs-ex-plicit () (values (with-output-to-string (*standard-output*) ; here (implicit)) (with-output-to-string (stream) (explicit stream)))) There is no real simple answer. My advice: Use stream variables, this makes debugging easier. They appear on the argument lists and are easier to spot in the backtrace. Otherwise you...
This is because Common Lisp is a Lisp-2. This means variables and functions reside in different namespaces. To use a function as a value, you must fetch it from the function namespace with the function function: (atom (function atom)) As a shortcut for this, you can prepend it with #'...
shell,batch-file,common-lisp,gnu-common-lisp
Maybe you could use multiple eval, here is what I do with sbcl. #!/bin/sh sbcl --noinform \ --eval '(load "boot.lisp")' \ --eval '(in-package #:my-pkg)' \ --eval "(do-something-useful)" # do-something-useful is in my-pkg ...
When evaluated, '(nil) and (list nil) produce similar lists, but the former can be considered constant when present in source code. You should not perform any destructive operations on a constant quoted list in Common Lisp. See http://l1sp.org/cl/3.2.2.3 and http://l1sp.org/cl/quote. In particular, the latter says "The consequences are undefined if...
It depends on what you want to do, but 1-3 are all viable options 1. CFFI To get this to work you will need to use FFI on both the go and lisp side. You will need to extern the appropriate function from Go as C functions, and then call...
macros,common-lisp,symbols,sbcl
That's a usual problem: what gets passed to a Macro? Compare calls like this: (symb 'book '-p) and (symb ''book '-p) Your macro form is this: (gen-predicate 'book) GEN-PREDICATE is a macro. classname is a parameter for this macro. Now what is the value of classname inside the macro during...
There are a few including: command-line-arguments -- http://cliki.net/command-line-arguments getopt -- https://github.com/keithj/getopt unix-options -- https://github.com/astine/unix-options I haven't actually tried any of them but hopefully one of them fits your needs....
If I understand correctly, you could store the values and associated symbols as pairs in the file, so something like this: (x . (1 2 3 4)) (y . (6 7 8 1)) And when you parse it out, use something like this: (let ((pair (read)) (set (car pair) (cdr...
append returns the new value, it does not modify its arguments. You need to use setq: (setq temp (append temp (list 1))) or push (which adds to the beginning of the list, not the end!): (push 1 temp) ...
list,recursion,lisp,common-lisp
Your code does not return NIL every time; it works well for simple lists. For instance (deep-find '(A B (C D)) 'A) returns T as it should. You have three cases in your cond: end of list, list head check, and list tail check. However, there is nothing regarding trees...
When you don't have any around methods, the order of method application is: all before methods from most specific to least specific, then the most specific primary method, and then the after methods from least specific to most specific. In your case you have two primary methods (methods without :before...
Like the comment above says, defclass does not evaluate arguments (it is a macro). While the usual advice is to avoid eval, I think that eval in this circumstance might be exactly what you want. While usually you would splice the form directly into some macro body, with defclass I...
This is a bug in clisp. Please report it on SF. Note that this works as it should on Ubuntu with the vanilla distribution.
In your above calls you are actually running the inner select, then splicing the results into the outer call. You should use sql-expressions instead of functions. If you (clsql-sys:file-enable-sql-reader-syntax) this can be accomplished with square brackets as follows. (select [books.bookid] :from [books] :where [in [books.bookid] [select [bookid] :from [bookauthors] :where...
You can use the cl-ppcre library for regular expressions. Its regex flavour understands the word boundary \b. The replacement could work like this: (cl-ppcre:regex-replace-all "\\bwa\\b" "ba wa walid" "") => "ba walid" I guess that you want to collapse any whitespace around the removed word into one: (cl-ppcre:regex-replace-all "\\s*\\bwa\\b\\s*" "ba...
list,parameters,lisp,common-lisp,clisp
Short answer: mapcan is destructive. As per the Hyperspec entry on quote, "The consequences are undefined if literal objects (including quoted objects) are destructively modified." The easiest way to fix this is not to use mapcan. (defun subAll (l k d) (cond ((and (atom l) (eq l k)) d) ((and...
I would use something like this: CL-USER 86 > (defmacro define-class (class-name) `(defclass ,class-name ()())) DEFINE-CLASS CL-USER 87 > (defun create-data (mode) (funcall (compile nil `(lambda () (define-class ,mode))))) CREATE-DATA CL-USER 88 > (create-data 'bar) #<STANDARD-CLASS BAR 402016CC73> Above uses code generation and the built-in compiler....
The problem of your "function one" is that you pass &rest parameter to the format function as is. Passing x to formatdf results in the creating a list containing one parameter, that is bound to allparm. So if you want to just print the first parameter you should write: (formatdf...
Experimenting with /bin/cat, I found some things that might help you. First, of all, always use finish-output after writing to the process's input: (format (process-input *cat*) "Hello~%") (finish-output (process-input *cat*)) Otherwise, the input may not reach the subprocess until you close the input stream. If Minecraft requires input before it'll...
tree,lisp,common-lisp,tree-traversal,postorder
I just figured that you have to call tree-depth not postorder. Though the answer basically stays the same: As soon as you use trace to get the call-tree: (trace tree-depth) (tree-depth '(1 (2 (4) (6)) (5 (7) (8)))) 0: (TREE-DEPTH (1 (2 (4) (6)) (5 (7) (8)))) 1: (TREE-DEPTH (2...
list,lisp,common-lisp,cons,cdr
In terms of what they do, car and cdr are equivalent to first and rest. This is quite clear in the documentation. The HyperSpec says on the entry for first, second, &c: The functions first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, and tenth access the first, second, third,...
Standard Common Lisp does not provide such a feature. I don't know of implementations which implement efficient arrays of structures, where the structures are allocated in the arrays. Implementations might implement efficient memory allocation for some structures, but usually not for vectors of structures. It would provide a challenge to...
lambda,crash,closures,common-lisp,allegro-cl
This is a compiler bug. Please contact Franz. CG-USER(8): (disassemble (lambda-producer)) ;; disassembly of #<Closure Template Function (:INTERNAL LAMBDA-PRODUCER 0)> ;; formals: X ;; closure vectors: ;; 0: #<Closure (LABELS LAMBDA-PRODUCER PRODUCER-LABEL2) @ #xdfdda7a> ;; 1: #<Function (LABELS LAMBDA-PRODUCER PRODUCER-LABEL)> ;; constant vector: 0: #<Closure Template Function (:INTERNAL (:INTERNAL LAMBDA-PRODUCER...
It's a lot easier than that. Remember that mapcar can take more than one list. Thus: (defun kmult-matrix (m k) (mapcar (lambda (list k) ;; inside here you get a list and ;; it's corresponding multiplier k ...) m k)) The result from the outer map would be a list...
Drawing the maze in the command is not the right approach. Putting a maze-stream into a global variable is also bad. ;-) The display pane has a :display-function. The idea is that after a command the whole application frame gets updated automagically. For example for :display-time :command-loop, the display pane...
(defun get-element (list name) (cadr (assoc name list :test #'string=))) ...
arguments,lisp,common-lisp,sbcl
You don't need anything beyond &rest: (defun divisible-p (number &rest divisors) "Check whether number is divisible by divisors." (dolist (spec divisors t) ; For each 'divisor' in divisors (etypecase spec ;; If divisor is a list, test if modulus of first value is 0 ;; then compare to second (boolean)...
python,shell,lisp,common-lisp,sbcl
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-ext:run-program "python" '("test.py") :search t :wait t) #<SB-IMPL::PROCESS :EXITED 42> CL-USER> (sb-ext:process-exit-code *) 42 ...
variables,binding,scheme,lisp,common-lisp
Because Common Lisp has a different namespace for functions, you need to use symbol-function or fdefinition. CL-USER> (defun foo (a) (+ 2 a)) FOO CL-USER> (defun debug-foo (a) (format t " DEBUGGING FOO: ~a" a) (+ 2 a)) DEBUG-FOO CL-USER> (defun debug-foo-again (a) (format t " DEBUGGING ANOTHER FOO: ~a"...
functional-programming,lisp,common-lisp
Common Lisp is multi-paradigm, not purely functional. There's really no reason to change your code algorithm from C#. If you want to write in a procedural style, you can continue to do so. (defun main (&rest args) (declare (ignore args)) (loop (princ "Enter the number:") (finish-output) (let ((name (read-line)) (numbers...
Here is a simple macro: (defmacro exec-and-report (form) `(format t "~S returns ~S~%" ',form ,form)) (macroexpand '(exec-and-report (+ 1 2))) ==> (FORMAT T "~S returns ~S~%" '(+ 1 2) (+ 1 2)) ; T (exec-and-report (+ 1 2)) ==> (+ 1 2) returns 3 NIL PS. I second @Sylvester's suggestion...
lisp,common-lisp,quicklisp,asdf
System For structuring large system use a system management tool. A 'free' one is ASDF. You would need a system declaration, which lists the parts of you library or application. Typically it goes into an own file. Then you load a system or compile a system. There should be tutorials...
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) ...
common-lisp,executable,sbcl,hunchentoot
(ql:quickload :hunchentoot) (use-package :hunchentoot) (defun main () (hunchentoot:start-server :port 8082) (sb-thread:join-thread (find-if (lambda (th) (string= (sb-thread:thread-name th) "hunchentoot-listener-1")) (sb-thread:list-all-threads)))) No explicit code is required to give you access to a REPL if you keep a terminal open (perhaps via GNU Screen). Send Ctrl+C to the terminal to break into the...
nconc destructively concatenates the results of each iteration. (print 'success) prints success and evaluates to success which is not a list. Try (defun finalcalc (tree1 tree2) (loop :for e1 :in tree1 :for e2 :in tree2 :collect (if (or (eq e1 'F) (eq e2 'F)) 'success 'failure))) ;; print result (mapc...
sockets,tcp,lisp,common-lisp,usocket
I don't think it's printing twice. I think it's printing (via write-line) once, and then write-line is returning the line, and the REPL is printing the return value. E.g., if you modify your mytest to return nil, I think you'll see the output that you're expecting: (defun mytest (command) (let...
Your define-class macro does not evaluate its class-slots argument. You can "fix" your code like this: (defmacro define-class (class-name class-slots) `(eval `(defclass ,',class-name () ,@(mapcar #'slot->defclass-slot ,class-slots)))) (macroexpand-1 '(define-class model '(nom id))) (defparameter *test* '(nom id)) (define-class model *test*) Note that you now have to quote the literal second argument...
You moslty already answered your own question, the difference is as you described. :some-symbol will be in :KEYWORD package, and 'SOME-SYMBOL will be in your current package CL-USER by default. In loop macro it's just a matter of taste. Some people prefer to use :for notation to get better syntax...
common-lisp,filenames,wildcard,clisp,pathname
It depends on the implementation, but for some, a backslash does in fact work. But because namestrings are strings, to get a string with a backslash in it, you have to escape the backslash with another backslash. So, for example, "foo?" is escaped as "foo\\?", not "foo\?". Last time I...
macros,common-lisp,reader-macro
You can try to use read and then look at what it returns: (defun string-reader (stream char) (declare (ignore char)) (let ((this (let ((*readtable* (copy-readtable))) (setf (readtable-case *readtable*) :preserve) (read stream t nil t)))) (etypecase this (string this) (symbol (symbol-name this))))) (set-macro-character #\@ #'string-reader) Above would allow @This and @"This",...
Lisp macros operate on arguments that have already been read by lisp's reader. Because of that a macro can't change how it's arguments are read, and if you pass an argument that will signal an error when you try to read it, e.g. 9:00 in (test 9:00), the reader signals...
Because you used (DEFVAR X 1), which declares X to be a global special variable. This then causes every other later binding of X to use dynamic binding: here in (LET ((X .... Style & Convention in Lisp Convention in Lisp: use *X* instead of X for special variables. (defvar...
Added this to parenscript's non-cl.lisp file: (define-expression-operator lisp-raw (lisp-form) `(ps-js:escape ,lisp-form)) (defun lisp-raw (x) x) Result: (ps (let ((x (ps::lisp-raw (json:encode-json-alist-to-string '((:a . 1) (:b . 2)))))))) "(function () { var x = {\"a\":1,\"b\":2}; return null; })();" ...
When I try to create a single-float array like this, i get: (make-array 2 :element-type 'double-float :initial-contents #(3.0 4.0)) Are you trying to create an array of single-floats (what your text says) or an array of double-floats (what the code suggests)? To create an array of double-floats, you'd use...
See CLHS 2.4.8.17 Sharpsign Plus To conditionalize reading expressions from input, Common Lisp uses feature expressions. In this case it has been used to comment out a form. It's a part of the reader. #+ looks if the next item, usually as a keyword symbol with the same name, is...
If you set non-terminating-p to t the macro character can appear unescaped within a symbol name (like # in the symbol foo#baz). If you leave it nil and the reader encounters the macro character while accumulating a symbol it terminates the collection of the symbol (like ' in foo'bar, which...
CL-USER 8 > (reduce #'min '((1) (-1) (3)) :key #'first) -1 or CL-USER 9 > (loop for e in '((1) (-1) (3)) minimize (first e)) -1 I fear getting the container element is more difficult: CL-USER 9 > (defun minimum (list predicate key) (when list (let* ((m0 (first list)) (m1...
What about this: (defun checkMember (L a) (car (mapcan #'(lambda (e) (and (equal a e) (list T))) L))) Note: it does not recurse into list elements, but the original function did not either....
(defun r (n f arg) "Calls the function F N times with ARG. Returns the arithmetic mean of the results." (/ (loop repeat n sum (funcall f arg)) n)) (r 6 #'d 6) dotimes does not return a value. it does: CL-USER 21 > (let ((sum 0)) (dotimes (i 10...
Arrays in Common Lisp can have fill pointers, which provides a way to treat an array as something which can grow and extend its length. Your 2-D array could be realized as an array of arrays with fill pointers. E.g.: (defun make-row-resizeable-array (rows max-columns) "Returns an array of length ROWS...
define in Scheme is not easily implemented in Common Lisp. It's not easily implemented in Scheme either. It's transformation is different in different scopes: (define test value) ; defines test, value can be anything even (lambda ..) which makes a procedur object (define (test arg ...) body ...) ; defines...
You can use eval: (mapcar #'eval *test*) However, this is a suboptimal solution. You would be much better off saving either a lambda: (defmacro make-model-lambda (args) (list* 'lambda () (apply #'make-model args))) (defparameter *test* (make-model-lambda ....)) (funcall *test*) or just the list of args themselves: (defparameter *test* (mapcar (lambda (name-id)...
A non-special variable's environment will be captured when you create a closure over it: (let ((x 1)) (let ((f (lambda () x))) (let ((x 2)) (eql 2 (funcall f))))) ;;=> NIL A special variable's lexical environment will not: (defvar *x*) ; *x* is special (let ((*x* 1)) (let ((f (lambda...
This is because cond is a macro. Try running the following: (macroexpand '(cond ((plusp 10) "positive") ((zerop 10) "negative") (t "zero"))) In SBCL, I get this: (IF (PLUSP 10) (PROGN "positive") (COND ((ZEROP 10) "negative") (T "zero"))) T In CLISP, I get this: "positive" ; T While I can understand...
list,function,recursion,lisp,common-lisp
This problem can be solved using the tortoise and hare algorithm where you have two cursors scanning though tortoise at one cons at a time and hare at double speed starting on the second element. If the tortoise and hare is the same element you have a cycle. (defun cyclicp...
sorting,char,comparison,lisp,common-lisp
A is a symbol, not a character. The string functions will accept symbols and use the symbol's name, but the character functions only accept true character objects, which are entered using #\ syntax. So you can do: (string< 'a 'a) (string< 'a 'b) For reference: #\a => character "a" =>...
SBCL has setf-able thread names, not IDs: (sb-thread:thread-name SB-THREAD:*CURRENT-THREAD*) ==> "main thread" What do you need the ID for?...
You could set up a single worker thread which receives instructions from a message queue. I have used chanl in the past for similar things, but there are several other options.
nil and () are two ways to express the same concept (the empty list). Traditionally, nil is used to emphasize the boolean value "false" rather than the empty list, and () is used the other way around. The Common LISP HyperSpec says: () ['nil], n. an alternative notation for writing...
Example 32.7. Calling Lisp from C: To sort an array of double-floats using the Lisp function SORT instead of the C library function qsort, one can use the following interface code sort1.c. The main problem is to pass a variable-sized array. extern void lispsort_begin (int); void* lispsort_function; void lispsort_double (int...
Strings are not immutable in Common Lisp, they are mutable: * is the last result from the Lisp listener CL-USER 5 > (make-string 10 :initial-element #\-) "----------" CL-USER 6 > (setf (aref * 5) #\|) #\| CL-USER 7 > ** "-----|----" CL-USER 8 > (concatenate 'string "aa" '(#\b #\b)) "aabb"...
clojure,scheme,lisp,common-lisp
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, you will get 2*3*1 = 6. We can write (product (product 2 3) (product)) = (product 6...
For defun without parameters, there should be an empty parameter list, like the following: (defun yn () (let ((ht (random 2))) (if (eq ht 1) (princ heads) (princ tails)))) ...
post,lisp,common-lisp,hunchentoot
Change your handler to this (define-easy-handler (test2 :uri "/test2") (name) (with-html-output-to-string (*standard-output* nil :prologue t :indent t) (:html (:body (:h1 (str name)))))) Then it should work. Read the cl-who documentation. Especially the information on local macros. I am including the relevant documentation here. A form which is neither a string...
Write a function that finds the start and end positions of the text you want to make invisible. Then use (put-text-property START END 'invisible t) to make the text invisible. For a quick-and-dirty example, use (search-forward "(declare (type " nil t). (match-beginning 0) is the start position of that sexp,...
post,common-lisp,sbcl,hunchentoot
You are passing a string to models, not a filter produced by $. Using eval on external input is a very bad idea, too. You could just pass the name to your handler and build the filter from that: (:p (str (models ($ "NOM" name)))) Call your handler with a1...
This isn't Common Lisp specific, but I think it may be a general help if you start by adding some debug output to your functions. E.g., in this case, if you print x and y before adding and calling digit-char-p with them, you can see that after the first two...
Apologies; not sure exactly where you're headed, and I don't know what the "grid" package is, so I couldn't test very much. It looks like you're trying to determine grid coordinates on the fly with '(* 2 1) but it's not working. If that's what you are doing, you could...
I'm usually using: (make-pathname :defaults old-path :directory (butlast (pathname-directory oldpath))) The :defaults argument makes sure that all relevant parts of the old pathname are being copied over....
You are working only with one field array. That's okay. But you better copy it when you push it to the fields list variable. Use copy-seq. Alternatively create a new field array once you pushed the old one onto the fields list....
This better formatting should make it easy to spot the error: (defun f (l) (cond ((null l) nil) ((listp (car l)) (append (f (car l))) (f (cdr l) (car (f (car l))))) (t (list (car l))))) If that does not help, use SBCL to compile the function. It will give...
profiling,common-lisp,profiler,sbcl
The columns are easier to interpret if you are familiar with the output of (free …) under SBCL (abbreviated): Evaluation took: 0.771 seconds of real time [ Run times consist of 0.061 seconds GC time, and 0.639 seconds non-GC time. ] 166,575,680 bytes consed In your example package:name was at...
(defmacro returnval (x) `(format t "~a has value ~a." ',x ,x)) CL-USER> (defparameter *forty-two* 42) *FORTY-TWO* CL-USER> (returnval *forty-two*) *FORTY-TWO* has value 42. NIL CL-USER> (let ((x 5)) (returnval x)) X has value 5. NIL If you really want that extra set of parens around the form, you can do...
Common Lisp has a powerful macro system. You can make new syntax that behave exactly the way you want them to behave. It's even expressed in it's own language making everything in the language available to transform the code from what you want to write to something that CL actually...
There are a few parts to this but here is a quick braindump PROFILE! Use a distribution of CL that has a profiler built in, I use sbcl for example http://www.sbcl.org/1.0/manual/Statistical-Profiler.html The nice thing about the sbcl profiler is that once you have profiled a function, if you disassemble it,...
lambda,macros,lisp,common-lisp,slime
Here's my implementation of the macro that generates the functions you initially listed: (defmacro defchord (name steps) `(defun ,name (foo scale) ,(concatenate 'string "Generates a " (symbol-name name) " on scale-step FOO of scale SCALE") (list ,.(mapcar (lambda (i) `(%part (+ foo ,i) scale)) steps)))) Some examples: > (macroexpand-1 '(defchord...
closures,common-lisp,dynamic-binding
First, a dynamic variable only takes its value from dynamic bindings, not lexical: (defun f () (declare (special x)) x) (let ((x 1)) ;; without this declaration, we would get an unbound variable error (declare (special x)) (f)) ;; => 1 You can achieve a default value for a local...
Assuming your y-or-n-p nil only when the user pressed n you could write your main loop as (defun main () (loop :while (y-or-n-p "Would you like to play again? ") :do (play-one-game))) or at the end of play-one-game, which I would rename play, prompt the user and if he wants...
You can use setf and the nth accessor: (defun replace-index (node index new-value) (setf (nth index node) new-value) node) (defparameter *orig* (list 1 2 3 4)) (defparameter *mod* (replace-index *orig* 2 99)) (list *orig* *mod*) ; ==> ((1 2 99 4) (1 2 99 4)) ...
First, note that the standard actually contains functions that will help with things like this. There's a useful count-if function that can take a predicate and count how many elements in a list satisfy it. For your case, you could do: CL-USER> (count-if #'(lambda (x) (> x 5)) (list 1...
emacs,common-lisp,slime,kubuntu
CHAMPAGNE !!! Reinstalled my VM, done all steps one by one and that wasn't ok. I had to launch twice the "bash <(curl -fksSL https://raw.github.com/overton..." command line once all other steps was executed and say "No" to the question asking if I want to create a personnal folder. Don't know...
I also think that could be some buffer problem. However, if you read from :stream line by line, that works fine: (let ((process (run-program "/bin/bash" (list "-c" "cat test.out") :output :stream :wait nil))) (loop for line = (read-line (process-output process) nil :eof) until (eq line :eof) do (print line))) ...
arrays,vector,types,common-lisp
A vector is an one-dimensional array. They are simple if they are not displaced, have no fill pointer, and are not expressly adjustable. A simple-vector also needs to be able to hold elements of any type. Which type-specifiers should I use? I guess that depends on what you want to...
lisp,common-lisp,gnu-common-lisp
(make-hash-table :test #'equal) ...
The problem is that C's stdio library has its own buffering that has nothing to do with Lisp's. Flushing the output requires you to have a pointer to C's FILE *stdout variable. You can get this pointer like this: (cffi:defcvar ("stdout" stdout) :pointer) Then, after using printf: (cffi:foreign-funcall "fflush" :pointer...
compiler-construction,common-lisp
Simplifying Common Lisp's go to other languages goto is, well, too much simplification. In Common Lisp, go can unwind the stack. For instance: (tagbody (mapc #'(lambda (el1 el2) (format t "el1: ~a, el2: ~a~%" el1 el2) (when (or (null el1) (null el2)) (go stop))) list1 list2) stop) If you're implementing...
Your understanding about the second function is correct. You may want to store actual nodes in the edges slot instead of node numbers. Then, instead of binding local variables to the node list inside of the two nodes that you want to connect, though, you can bind them to the...
Not sure there's a way to get a list of accessors easily. The object inspection functions tend to be exported from implementation-specific packages. You can take a look at the package file of cl-mop to see where they are. The relevant lines are ... (:shadowing-import-from #+openmcl-native-threads #:ccl #+cmu #:pcl #+sbcl...
common-lisp,hunchentoot,parenscript
This will happen if you have both the defpsmacro and the use of the defined macro in the same file. I haven't had the time to dig into parenscript code deep enough to understand what exactly goes wrong with the order of evaluation, but the end result is that, when...