question_id
int64
1.99k
74.6M
answer_id
int64
4.76k
74.6M
title
stringlengths
20
144
question
stringlengths
22
4.09k
answer
stringlengths
24
4.1k
4,086,317
4,086,414
Is there any benefits to learning LISP?
I am a pretty experienced Ruby, Objective C, and Java programmer and I was watching a video on emacs (because I have been using Vi) and noticed that it is also a LISP interpreter. That spiked my interest, and brought up an interesting question: For someone that knows modern high level languages such as Ruby, Java ,and ...
There are definitely benefits to learning a language built on a different paradigm from the one you are used to (which I note are merely object oriented with strong imperative roots). LISP is the granddaddy of functional languages (one of my favourites, Scheme, is a LISP dialect). Besides widening your horizons, funct...
4,093,168
4,093,214
Adding numbers from a list (e.g. asdf125dkf will return 8)
I need a function that will take in a list of characters and numbers, and then return the numbers added up (ignoring the characters). This is what I have so far: (define (adder lst) (cond ((null? lst) 0) ((number? (car lst)) (+(adder (car lst)) (adder (cdr lst)))) ((char? (car lst)) ...
In your second cond case, there's no reason to run adder on (car lst). Just adding (car list) itself to the recursive step should work. For the last line, don't test (char? (car lst)). Just make the last line the else clause, meaning that anything BUT a number will go to the else line. The reason you're getting void ...
4,093,845
4,094,137
Is there a common lisp macro for popping the nth element from a list?
I'm pretty fresh to the Common Lisp scene and I can't seem to find an quick way to get the nth element from a list and remove it from said list at the same time. I've done it, but it ain't pretty, what I'd really like is something like "pop" but took a second parameter: (setf x '(a b c d)) (setf y (popnth 2 x)) ; x is ...
I came up with a solution that is a little more efficient than my first attempt: (defmacro popnth (n lst) (let ((t1 (gensym))(t2 (gensym))) `(if (eql ,n 0) (pop ,lst) (let* ((,t1 (nthcdr (- ,n 1) ,lst)) (,t2 (car (cdr ,t1)))) (setf (cdr ,t1) (cddr ,t1)) ,t2)))) Here is i...
4,094,615
4,094,662
Resources for learning Lisp
What is a good book or tutorial to learn Lisp?
Practical Common Lisp is an awesome (and free) book for anyone interested in the language.
4,096,015
4,096,353
Which dialect of LISP is 'Paradigms of Artificial Intelligence Programming' written in?
What version/dialect/implementation of LISP is this meant to run on? (I do understand that the book is written in Common LISP (as specified in the introduction) and that it predates the 1994 CL standard). Split question into two to make it clearer.
If we look at auxfns.lisp found on Peter Norvig's web page for the book, there's this bit (eval-when (eval compile load) ;; Make it ok to place a function definition on a built-in LISP symbol. #+(or Allegro EXCL) (dolist (pkg '(excl common-lisp common-lisp-user)) (setf (excl:package-definition-lock (find-pack...
4,110,742
4,110,932
Is there a common LISP function to compare the contents of two lists?
In particular, I just want to ensure that two lists have the same elements, ignoring order
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
4,116,069
4,116,700
Lisp: multidimensional array elementwise operations
What is the "correct" construct in Common Lisp to apply elementwise operations to multidimensional arrays? The following examples should help illustrate what I'm trying to do: A) Suppose I want to increase every element of an array by one: 0 1 2 1 2 3 3 4 5 -> 4 5 6 6 7 8 7 8 9 B) Suppose I want to add 2 arrays...
There are four ways to do that: Write an ARRAY-MAP function based on the array dimensions and iterate over those. Use ROW-MAJOR-AREF, which views the array like a vector. Use displaced one-dimensional arrays for the operations. Example for a use of displaced arrays: (defun array-map (function &rest arrays) "maps th...
4,117,994
4,118,141
How does append work in Common Lisp?
I just started learning Lisp and I don't seem to understand the following piece of code: (setf y (loop for x in y for z = (something) unless (condition for z) append z)) Where is z appended?
It is appended to an unnamed list to be returned when the loop terminates. As first approximation, you may think of it as a shorthand for (loop ... append z into result finally (return result)) The append here is a loop keyword; it's not related to the append function, except for sharing the same name --- so it's the...
4,119,509
4,119,546
How to read sexp from file
If I write a file using (with-open-file (s "~/example.sexp" :direction :output) (write '(1 2 3) :stream s) (write '(4 5 6) :stream s) (write '(7 8 9) :stream s)) A file is created containing (1 2 3)(4 5 6)(7 8 9) But when I attempt to open and read it using (setf f (open "~/example.se...
You got the arguments to READ wrong. It should be simply (read f), not (read :input-stream f).
4,120,973
4,121,584
Returning a List of Words from a File
My next project is writing a hangman game. I figured it would help me brush up on strings and file I/O. Currently, i'm stuck on reading in a file of strings into a list. I'm trying to avoid global variables, so could someone point me in the right direction to make this (probably broken) code into a function that return...
You can read-in the words as Lisp symbols, with just a few lines of code: (defun read-words (file-name) (with-open-file (stream file-name) (loop while (peek-char nil stream nil nil) collect (read stream)))) Example input file - words.txt: attack attempt attention attraction authority automatic awa...
4,121,544
4,157,294
How do I create a dynamically loadable version of RtAudio in OSX
I need to create librtaudio.dylib, a dynamically loadable RtAudio library (http://www.music.mcgill.ca/~gary/rtaudio/). I'd like to write CFFI bindings (in SBCL) but I can't seem to compile a dylib that is loadable using CFFI. Here are the compiler and linker commands I'm using to create the dylib: g++ -O2 -Wall -Iinclu...
It seems that CFFI's C++ support is not adequate to load RtAudio, as it is a C++ lib. A workaround is to write a C wrapper around RtAudio and then write bindings to the wrapper.
4,128,993
4,130,522
Consolidated cond arguments in Clojure (CL style)
In Clojure I do this (println (cond false "don't care" "otherwise" "otherwise")) In Common LISP this would be (print (cond (nil "don't care") ("otherwise") )) Is there a way to get this kind of simplified cond in Clojure?
Version which includes a fix for that Alex Taggart noticed below. Passes all the test cases shown in the test. It allows for arbitrary clauses passed to my-cond to be length 1 instead of 2 which results in the length 1 clause being both the test for truthiness and the result if it is true. Based off my limited experien...
4,135,820
4,136,381
Is Clojure considered Lispy or Schemey?
Possible Duplicate: Is Clojure closer to Scheme or Common Lisp from a beginner's perspective? Clojure is a Lisp dialect that runs on the JVM. Is Clojure considered Lispy or Schemey? I mean is it like Lisp or like Scheme?
If by "Lispy" you mean "like Common Lisp," then... Clojure is "Lispy" because it has full (nonhygienic) macros and doesn't guarantee tail-call optimization (except with recur). Clojure is "Schemey" because it has a single namespace for both functions and values (it is a Lisp-1).
4,138,294
4,139,024
First Lisp with macros?
McCarthy's original Lisp and some number of incarnations thereafter did not have a macro facility like we now have in Common Lisp, Clojure, Scheme, etc... This I know. However, it is unclear to me exactly how macros came to be, what implementation(s) had them first, and what motivated them. References to papers and...
From The Evolution of Lisp (PDF): Macros appear to have been introduced into Lisp by Timothy P. Hart in 1963 in a short MIT AI Memo [Hart, 1963] See: AIM-57 Author[s]: Timothy P. Hart MACRO Definitions for LISP October 1963 ftp://publications.ai.mit.edu/ai-publications/0-499/AIM-057.ps ftp://publications.ai.mit.edu/ai...
4,154,646
4,154,865
What is the relationship between a Lisp "association list" and a key-value mapping like Java's Map?
I'm reading Land of Lisp (which is by the way, one of the best technical books I have ever read) and I have come across the "association list": (defparameter *edges* '((living-room (garden west door) (attic upstairs ladder)) (garden (living-room east door)) (attic (living-room downstairs l...
Yes, the association list is one way to express key-value associations. Other structures Common Lisp provides to that end are property lists and hash tables. The value is actually already contained in a list. An alist is fundamentally a list of pairs, where the car of each pair is the key, and the cdr is the value ass...
4,158,854
4,162,027
Count positive elements in list
Im trying count the number of positive elements in a list. Here is what I have so far: (define howMany (lambda (list) (cond [(not (list? list)) 0] [(null? list) 0] [(> list 0) (+ 1 (howMany (cdr list)))]))) It keeps giving me an error, "expects type real number", how would you fix...
There are a couple of bugs in your code. (> list 0) should be (> (car list) 0) as you want to check if the first element of the list is greater than 0. You cannot apply the default implementation of > to a list either. (+ 1 (howMany (cdr list))) will also fail as howMany does not always evaluate to a number. You have ...
4,160,412
4,160,442
Compiling SBCL from source on Mac OS X
I need to compile SBCL from source and enable threading on my Mac OS X MacBook. I've done the following: Downloaded sbcl-1.0.29.54.rc1 and unzipped it to the root of my hard drive (I could put it somewhere else if that would be better). Invoked sh make.sh It does a lot of stuff (it doesn't fail immediately), but th...
there is a patch located at this bug report . Try applying it and rebuilding cleanly (make clean && ...) then see if the problem goes away.
4,164,336
4,164,565
Does Common Lisp has great legacy? (Is it better to learn Common Lisp or a more modern variant such as Scheme?)
I want to learn some language from Lisp family. It may be CL or Scheme and try to use it for web programming. Just for fun. I have significant C++ experience (prefessional development). But I want my choice be modern language without legacy (in language itself and library), because I want learn good design patterns fro...
Common Lisp has a lot of idiosyncrasies, several of them probably stemming from legacy (don't know my Lisp history well enough to say for sure). There's quite a few warts such as inconsistencies in function nomenclature and and argument orders. But the actual language itself is, although a bit odd in places, rather san...
4,164,659
4,166,177
GNU emacs: setting keybindings to highlight text with shift key
I'm trying to set some keybindings to use the Shift key to highlight text. I could use pc-selection-mode, but that doesn't offer all the key bindings I want. For example, I'd like to be able to shift-mark an entire paragraph by pressing Shift-Ctrl-down which I can do in most MS text editors, but pc-selection-mode doesn...
Under GNU Emacs, the key binding should look like (global-set-key [(shift right)] 'shift-mark-forward-char) ([…] constructs a literal array). But I suspect you're going at this the wrong way. Are you running GNU Emacs, XEmacs, or both? What versions? Unless you're running extremely old versions, pc-selection-mode shou...
4,169,566
4,169,672
Lisp Recreating a Temporary Variable
I'm having a bit of trouble with Lisp. What i'm attempting to do, is keep track of the amount of times a number appears in x number of lists. However, running this over and over again, lisp isn't recreating the variable, but using the ending value from the last time I called the function. So I'm wondering how can I ...
Change the initialization form for VAR to be an expression that creates new lists, such as (make-list 10 :initial-element 0) or even (list 0 0 0 0 0 0 0 0 0 0). Basically, do not ever use quoted objects if you have intentions on modifying them, as the consequences are undefined if you do. In fact, evaluating that func...
4,170,829
4,170,889
Does the defstruct instantiation evaluate its &key arguments?
I'm trying to nest instances of defstructs as some of the &key arguments in another defstruct, as demonstrated by the code below. For some reason, the variables to be inserted into the outer defstruct are not being replaced with the values bound to them (which is what I would expect). Any explanations and/or suggestion...
'(y my) is a literal list that contains two symbols - Y and MY. (list y my) would create a list of two values, first is the value of variable Y, second is value of variable MY. The same is with '('John x) - it's a literal list that contains two elements - a list (QUOTE JOHN) and symbol X.
4,178,806
4,180,984
Storing dates in Common Lisp
What's the proper way to store dates in Common Lisp? The closest thing I found to an answer is this, which doesn't really seem to cut it for me.
How about ENCODE-UNIVERSAL-TIME? (defparameter *my-birth-date* (encode-universal-time 0 0 0 14 2 1984))
4,187,822
4,192,781
Performance difference between functions and pattern matching in Mathematica
So Mathematica is different from other dialects of lisp because it blurs the lines between functions and macros. In Mathematica if a user wanted to write a mathematical function they would likely use pattern matching like f[x_]:= x*x instead of f=Function[{x},x*x] though both would return the same result when called wi...
The way I understand Mathematica is that it is one giant search replace engine. All functions, variables, and other assignments are essentially stored as rules and during evaluation Mathematica goes through this global rule base and applies them until the resulting expression stops changing. It follows that the fewer t...
4,190,178
4,190,339
What is the best lisp/scheme for unix scripting?
The title pretty much says it all. I use clojure for my major projects but it's not a good scripting language because the jvm has a slow startup and doesn't interface well with some unixy things. So I'm looking for a lisp which will work well as a scripting language, for example having a good interface for managing uni...
Scsh (it stands for "Scheme shell") can be gotten at http://www.scsh.net. It's "a variant of Scheme 48 (an R5RS compliant new-tech Scheme system) ... designed for writing real-life standalone Unix programs and shell scripts." A nice introduction to system administration in it can be found at http://www.theillien.com/Sy...
4,194,620
4,194,985
Current memory usage in Lisp
I need to find out, from within a Common Lisp program, how much memory is currently being used. I'm given to understand there is no portable method (the standard function room prints the information to standard output in text form instead of returning it as a value), but sb-kernel:dynamic-usage works in SBCL. What are ...
It may not help you much, but anyway: You can capture the output of (room) and parse it. (with-output-to-string (*standard-output*) (room)) Above returns a string with the output of ROOM. Additionally it may help to request the memory size of the process via an external call to a standard unix command (if you are on...
4,195,637
4,195,783
Why doesn't a LISP defun require a quote before its parameter argument?
Take this function: (defun sum-greater (x y z) (> (+ x y) z)) It's my understanding that in LISP the first element in a list always represents a function to be performed on the subsequent atoms/lists. So why doesn't LISP treat the x in (x y z) as a function to be performed on y and z. Clearly this would not be desira...
IIRC in Common Lisp at least defun is a macro (HyperSpec), meaning it may define any evaluation strategy whatsoever for its arguments.
4,203,174
4,203,576
Lisp: decode-universal-time is void
I'm editing orgmode, a time-management mode for emacs. I'm adding some new time functions. In my addition i need to determine the day of the week for a given date. I use the following code: (defun org-day-of-week (day month year) (nth-value 6 (decode-universal-time (encode-universal-time 0 0 0 day month year 0) 0))) E...
Essentially decode-universal-time is a Common Lisp function. These are not necessarily available in emacs-lisp. Some quick checking in a local emacs indicates that this is the fact. Something close to your original implementation would be: (defun org-day-of-week (year month day) (nth 6 (decode-time ...
4,213,075
4,214,756
Running inferior lisp
After installing emacs and attempting to run-lisp, I get Searching for program: No such file or directory, lisp Then, after trying to run lisp again, a new inferior-lisp buffer opens, but if I try entering anything in, I get: Output file descriptor of inferior-lisp<1> is closed Any idea what I should do?
You should set inferior-lisp-program variable. Be careful 'cos some Lisps expect the core to be specified in the command-line or reside in a current directory (especially if you're on Windows). If you're a newbie and just want to get familiar with lisp there is a built-in Elisp interpreter. Run M-x ielm to get it.
4,215,373
4,215,679
Implementing basic library functions in LISP (manually)
Is there any way by which I can define functions my_list, my_cons, my_append which perform similar function as list, cons and append respectively? Otherwise where can I find the implementation of these functions? Thanks
For my_list and my_append, the solutions are: (defun my_list (&rest arguments) `(,@arguments) ) (defun my_append (a_list an_item) `(,@a_list ,an_item) ) (my_append (my_list 'a 'b 'c) 'd) I'm probably wrong but I dont know any alternative method to make pairs, so an alternative to cons do not seems possible. ...
4,220,349
4,220,411
Basic LISP recursion, enumerate values greater than 3
I need a recursive LISP function that enumerates the number of elements in any list of numbers > 3. I'm not allowed to use lets, loops or whiles and can only use basic CAR, CDR, SETQ, COND, CONS, APPEND, PROGN, LIST... This is my attempt at the function: (defun foo (lst) (COND ((null lst) lst) (T (IF (> (CAR ls...
Your code is pretty close to correct, just a small mistake in the base case: For the empty list you return the empty list. So if you have the list (6), you add 6 to foo of the empty list, which is the empty list. That does not work because you can't add a number to a list. You can easily fix it by making foo return 0 i...
4,222,177
4,222,192
Lisp function call error
I've written a Lisp function like this: (defun power (base exponent) (if (= exponent 0) 1 (* base (power (- exponent 1))))) When I try to call it, however, I get some errors: CL-USER 2 > (power 2 3) Error: POWER got 1 arg, wanted at least 2. 1 (abort) Return to level 0. 2 Return to top loop level 0. ...
It is the recursive call that only has one argument: (power (- exponent 1)) It should be like this: (power base (- exponent 1))
4,222,190
4,222,217
LISP car of the last element?
LISP stumps me yet again... Why can't I get the value of the last element in a list? I have a list set up similar to this: (setq bar '(((1 2) 3 4 5)((6 7) 8 9 10))) Now I get a return of 4 for: (caddar bar) There is a return of (5) for: (cdddar bar) But I can't get a 5 for: (cadddar bar) Why is this--and how do I g...
The functions with 5 or more a's and d's are not defined. Only 4 and fewer. There are too many possible functions of that length for it be be practical. You have to just spell it out: (car (cdr (cdr (cdr (cdr (car x))))))
4,226,948
4,227,234
Why does (list 'quote 'x) evaluate to 'x and not ('x) or (quote 'x)?
I'm trying to learn LISP and was going through a code example where something similar to the following code is used: (list 'quote 5) This evaluates to '5 in the REPL. I expected it to evaluate to ('5) or (quote 5) I'm trying this out in the CLISP REPL. Any help would be appreciated.
The read-evaluate-print loop first reads, then evaluates 'quote is read as "the symbol whose name is QUOTE" 5 is read as "the number 5" So (list 'quote 5) is evaluated as "make a list whose first element is the symbol whose name is QUOTE and whose second element is 5". The result of this evaluation can be written as "(...
4,235,817
4,235,848
Lisp Format Procedure
Im starting to program in Lisp and having an hard time with the Format function. My objective is to print a list of integer sublists as N integers for line. For example: '((1 2 3) (4 5 6) (7 8 9)) should be printed as 1 2 3 4 5 6 7 8 9 I tried using iteration in the format procedure, but I failed. What I wrote was: ...
(format t "~{~%~{~A~^ ~}~}" '((1 2 3) (4 5 6) (7 8 9))) prints 1 2 3 4 5 6 7 8 9
4,244,763
19,890,681
What's the equivalent of foldr, foldl in Emacs Lisp?
What's the equivalent of foldr, foldl in Emacs Lisp?
Since Emacs-24.3 we recommend the use of cl-lib over cl (which is planned for removal in some distant future), so it would be: (require 'cl-lib) (cl-reduce #'+ '(1 2 3 4)) and since Emacs-25, you can also use the seq package for that: (require 'seq) (seq-reduce #'+ '(1 2 3 4) 0)
4,250,913
4,251,008
REPL on console emacs
I'm using the console version of emacs (that is, I'm SSH'ed into a remote machine and using emacs there) and I was wondering how (assuming it's possible) to start up the REPL from there. I'm pretty new to Lisp and emacs.
How about M-x ielm? ielm: Inferior Emacs Lisp Mode
4,251,185
4,251,512
Determine level of searched element
Given a list/tree of the form : (node1 (node2) (node3 (node4) (node5)) (node6)) I should be able to find out the depth at which a searched node resides. This is what I've done so far: (defun search-it (lst level n) (cond ((null lst) nil) ((and (atom (car lst)) (equal (car lst) n)) level) ((atom (ca...
Generally return a list of depths. So, if an item is found, then return the list of the single depth. If you branch to the first and rest of the list, then don't 'cons', but 'append'. Note also that your code does not find all depths. CL-USER 6 > (search-node '(6 (6)) 6) 0
4,252,306
4,252,811
SLIME on the console
Does anyone have experience using SLIME on the console for Common Lisp? I'm trying to go through Practical Common Lisp but the commands in the book don't seem to work for the console version of emacs/SLIME. I suppose my question is: is there somewhere where I can find documentation specifically for SLIME on the console...
I assume you mean running slime with emacs on the console ie without X. Slime requires emacs (eq slime 'superior lisp interaction mode for emacs'). I ran it with emacs -nw (which just uses the terminal and no X facilities that I can see) and slime worked fine (Ubuntu 10.04 X86-64). Are you running in emacs? Is slime s...
4,253,754
4,256,487
Emacs lisp: is it possible (using lisp) to run command in an eshell working in other buffer?
I have a running instance of eshell in one buffer and I am writing c++ source in another one. I had bound compile to <F5> and I am wondering if it is possible to run the output file (of the compilation) in an eshell instance running in another buffer? If not, then maybe there is a way to open eshell in new frame and a...
Normally, if you want to run something after the compilation has finished, you add it to the compilation command. For example, instead of M-x compile RET make RET You might type M-x compile RET make && ./test RET or you might add the program to some appropriate target in your makefile, so you can do M-x compile RET m...
4,267,288
4,267,311
Is there a better way to get the nth item in a list?
The following two expressions are equivalent: (third (list 1 2 3 4)) (first (nthcdr 2 (list 1 2 3 4))) However, using "third," "fourth," "fifth," etc. isn't always practical and (first (nthcdr n list)) seems a little verbose. Is there a way to say something like (item 2 (list 1 2 3 4)) to get the nth item in a list?
(nth 3 (list 1 2 3 4)) returns 4th item (zero based!) According to the HyperSpec: Accessor NTH Description: nth locates the nth element of list, where the car of the list is the “zeroth” element. Specifically, (nth n list) == (car (nthcdr n list)) Examples: (nth 0 '(foo bar baz)) => FOO (nth 1 '(foo bar baz)) => ...
4,273,461
4,273,531
Comparing lists in Lisp
I could figure out some way to do this myself but I have a feeling there's a simpler, perhaps built-in way to do this. I want to see if any two lists share an element. These are the two lists I'm dealing with at the moment: ((0 1 2) (3 4 5) (6 7 8) (0 3 6) (1 3 7) (2 4 8) (0 4 8) (2 4 6)) ((0 1 7) (0 1 6) (0 1 3) (0 3...
How about INTERSECTION? (defvar a '((0 1 2) (3 4 5) (6 7 8) (0 3 6) (1 3 7) (2 4 8) (0 4 8) (2 4 6))) => A (defvar b '((0 1 7) (0 1 6) (0 1 3) (0 3 7) (0 3 6) (0 6 7) (1 3 7) (1 3 6) (1 6 7) (3 6 7))) => B (intersection a b :test 'equal) => ((1 3 7) (0 3 6))
4,283,899
4,284,266
Lisp macro set-or-nconc not able to get working
I were trying to make a macro for binding variable if it is unbind, else if it is binded than appending to its value (defmacro set-or-nconc (var &rest args) `(,(if (and (boundp var) (not (null ,var))) 'nconc 'setq) ,var ,@args)) The intended output wanted is (set-or-nconc i '(a b))...
The determination as to use setq or nconc is done at macro-expansion time, not at run-time. This is a bit of a problem. There's also some issues with your backquote expression, as there's either a "," too many (in (null ,var)) or one too few (in (boundp var), with the need for another backquote). Below is at least some...
4,287,586
4,294,958
Advantages of different Scheme R6RS implementations
I'd like to start programming in Scheme but the variety of different implementations is confusing. What are some advantages or disadvantages of various implementations?
Every implementation tends to focus on something different. Racket emphasizes its large libraries as "batteries included", while Ikarus Scheme touts itself as compiling fast code. You should examine implementations based on what you want. If you're just learning Scheme, DrRacket is a good choice with its friendly in...
4,288,292
4,288,311
Get index of list within list in Lisp
If I have a list like this ((0 1 2) (3 4 5) (6 7 8) (0 3 6) (1 3 7) (2 4 8) (0 4 8) (2 4 6)) And I want to find the index of (0 3 6), is there a built-in function to do this? POSITION doesn't seem to work when the search item is itself a list.
See hyperspec. POSITION can take a :test argument: (position '(0 3 6) '((0 1 2) (3 4 5) (6 7 8) (0 3 6) (1 3 7) (2 4 8) (0 4 8) (2 4 6)) :test #'equal)) 3 The default test for POSITION (and other sequence operations) is EQL, by the way.
4,288,339
4,288,515
How do you comment out all or part of a Lisp s-exp using Paredit?
When editing Lisp code, occasionally it's useful to entirely comment out a top-level definition, like this: ;(defun some-fn-which-is-broken (x) ; ...) ... or comment out only part of an s-expression, like this: (foo x ; y z) ... and then recompile the file and test something in the REPL, etc. With paredit-mo...
Position the point on the first character of the whole sexp, mark the whole sexp with C-M-space, and issue M-; to do the commenting. If it is necessary to do so, your source code will also be re-formatted so that only the sexp you marked, and nothing that was also on the same line, is in a comment. You can very easily ...
4,290,354
4,290,397
from-end does not work as I expect in position
[18]> (position 3 '(1 2 3 4 5 6 7 8) :from-end nil) 2 [19]> (position 3 '(1 2 3 4 5 6 7 8) :from-end t) 2 What to do?
position is working as documented. The hyperspec says: The position returned is the index within sequence of the leftmost (if from-end is true) or of the rightmost (if from-end is false) element that satisfies the test; otherwise nil is returned. The index returned is relative to the left-hand end of the entire sequen...
4,294,346
4,294,568
Difference between lists and arrays
It seems a list in lisp can use push to add another element to it, while an array can use vector-push-extend to do the same thing (if you use :adjustable t, except add an element at the end. Similarly, pop removes the first item in a list, while vector-pop removes the last item from a vector. So what is the difference ...
A vector is tangible thing, but the list you're thinking of is a name for a way to view several loosely-connected but separate things. The vector is like an egg carton—it's a box with some fixed number of slots, each of which may or may not have a thing inside of it. By contrast, what you're thinking of a list is more ...
4,295,759
4,296,072
How to make this neighbor function?
I have this code: (defparameter fc #\F) (defparameter bc #\B) (defparameter gap #\G) (defun solp (seq) (if (eql fc (car seq)) (not (if (listp (cdr seq)) (find bc (cdr seq)) (eql seq bc))) (solp (cdr seq)))) (defun heuristic (seq &optional (f 0)) (if (eql nil ...
All the functions from the CLHS chapters conses and sequences apply to lists. Also note that it is good programming style (for various reasons) to name global variables like this: *bc*, *fc* and *gap*. Also note that in newer code one often uses FIRST instead of CAR and REST instead of CDR.
4,296,407
4,296,433
clisp : remove from list of list
(remove '(1 2) '((1 2) (1 3))) doesn't remove '(1 2) from list in common lisp. (I think it uses eq and not equal). Do we have any other alternative to delete element from list of lists in common lisp?
(remove '(1 2) '((1 2) (1 3)) :test #'equal)
4,296,895
4,296,912
Correct usage of destructuring-bind
I'm experimenting with destructuring-bind as follows: (destructuring-bind (a b) '(1 2) (list a b))) When I evaluate this in the REPL I get: READ from #1=#<INPUT STRING-INPUT-STREAM>: an object cannot start with #\) [Condition of type SYSTEM::SIMPLE-READER-ERROR] I expected the result to be (1 2) The ...
Remove the extra ) on the end. Works fine.
4,304,268
4,304,359
Merge 2 sorted lists
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. The...
Merging two sorted lists is algorithmically trivial. Start with the first element of each list, compare, write the lower one to output and advance the list where you found the lower one. Keep going until you reach the end of one list, then put out the remainder of the other list. This requires just one loop over each l...
4,304,925
4,305,006
Why is it customary to put many closing parentheses on one line in Lisp-based languages?
Usually code looks like this: (one-thing (another-thing arg1 (f arg5 r)) (another-thing arg1 (f arg5 r))) Why doesn't it like this?: (one-thing (another-thing arg1 (f arg5 r)) (another-thing arg1 (f arg5 r)) ) It allows adding and removing "another-thing" lines more easily (without removing and re-add...
There are two points to be made here: Conventions are important in themselves. Sticking to wrapped parens means your code is more readable by other lisp programmers, and if you adopt this style you will also develop practice at reading theirs The advantages of splitting )s onto their own lines are not actually advanta...
4,318,309
4,318,554
Console I/O in Common Lisp
In Common Lisp, I am writing a console application. I've finished most of the code, but two critical pieces are still confusing me. How to read a key from the input and get the ascii code for it. How to display an ascii character, without special formatting or newline. On the second, I've tried: (print (code-char 69)...
See read-char and write-char in the streams CLHS chapter. READ-CHAR reads a character. Portable Common Lisp does not have the capabilities to read 'keys', but it can read characters from a stream. For getting the code of a character see char-code.
4,320,166
4,323,084
How to stream program code?
I've been learning Lisp recently (Scheme, Racket and Clojure to various extents) and have read the corresponding literature on famous Playstation developer Naughty Dog. This Gamasutra article mentions that their streaming engine actually streams in game code in addition to game data. Now, they have a dialect of Lisp ca...
A 'Listener' in Lisp speak is a REPL (Read Eval Print Loop). Listeners usually provide several services like integrated debugger, command histories, command interpreters, and more. In many cases the Listener runs inside the Lisp one is using. In some cases all code typed to a REPL/Listener is compiled before executing ...
4,346,908
4,346,987
Learning Common Lisp tips for a Windows/C++ programmer
I'm an experienced C++/.NET/Java Windows/web programmer trying to learn (Common) Lisp. I'm reading Practical Common Lisp and using SLIME. I'm getting the Lisp language easily enough, but I'm having trouble groking the mechanics of development. One of my issues is dealing with Emacs. I have no experience with it and fin...
-I get the REPL, but don't quite get how I can use it effectively. When I need to change a function I have to retype the defun and make changes (tedious and error prone). How can I do this better? -How do I get from entering code at the REPL to actually having a program? I'm used to the C model where you hav...
4,350,607
4,351,756
Is it possible to generate 40,000+ element of Fibonacci recursively in Lisp?
I'm trying to solve Project Euler question 2 with Lisp. This recursive solution blows the stack on execution, but I thought Lisp (using clisp) would recognize the tail recursion. This is being entered into the top-level. (defun e2-problem (&optional (f1 1) (f2 1) (sum 0)) "Sum fibonacci sequence, even terms up to ...
1) Correct syntax error in code: (defun e2-problem (&optional (f1 1) (f2 1) (sum 0)) "Sum fibonacci sequence, even terms up to 4 million" (if (> f2 4000000) sum ;; here was a closing bracket (e2-problem f2 (+ f1 f2) (if (evenp f2) (+ sum f2) ...
4,353,053
13,473,368
Is out there anything that is for s-expressions what XPATH is for XML?
I am looking for a common-lisp impl if possible. (Also, I dont want to convert sexp to XML and use xpath on the result.)
a bit late answer, but it seems http://www.cliki.net/spath is exactly what you look for.
4,354,681
4,355,311
How to define structures in Lisp using parameters in the definition
I want to write some Lisp code like this (defstruct board (size 7) (matrix (make-array (list size size)) (red-stones 0) (black-stones 0)) in order to define a structure that represents a game's board. I want to be able to create a new board with make-board that will create the matrix on the fly with th...
You could use a boa constructor: (defstruct (board (:constructor make-board (&optional (size 7) &aux (matrix (make-array (list size size)))))) (size) (matrix) (red-stones 0) (black-stones 0)) CLHS documentation for defstruct and BOA lambda lists.
4,357,257
4,357,382
Binary Search Tree in Scheme, trying to use Dr. Racket to simply return true or false if value is present in BST. Error
I'm using Dr. Racket, language Pretty Big, and I'm trying to make a simple binary search tree "in?" method, that will return if a value is in the binary search tree or not. It needs to be general, accepting any kind of search tree (whether it contain strings, ints, etc.), but I'm running into this error message that is...
One problem is you have your < and > reversed. Assuming you want your left sub tree to be the smaller, then (< value (car tree)) should call again with the (cadr tree). Also you should use #t instead of (#t).
4,361,705
4,363,370
Unbound Variable on Function Name
I'm writing a program in Lisp(common lisp dialect).. I want the program to count the number of sublists in a list.. This is what I have written till now: (defun llength (L) (cond ((null L) 0) ((list (first L)) (progn (+ (llength (first L)) 1) (llength (rest L)))) ((atom (f...
(defun llength (list) (cond ((null list) 0) ((listp (first list)) ;; 1 + the count of any sub-lists in this sub-list + the ;; count of any sub-lists in the rest of the list. (+ 1 (llength (first list)) (llength (rest list)))) (t (llength (rest list))))) Test: > (llength '(1 2 3 4...
4,366,668
4,366,925
str_replace in Common Lisp?
Is there some function similar to PHP's str_replace in Common Lisp? http://php.net/manual/en/function.str-replace.php
There is a library called cl-ppcre: (cl-ppcre:regex-replace-all "qwer" "something to qwer" "replace") ; "something to replace" Install it via quicklisp.
4,374,158
4,374,324
Lisp Reverse "all" Function
I want to write a function in lisp that reverses all elements from the list using map functions but I don't have any idea how to start this.. I think I have to use the built in reverse function somehow.. For example if I have the list (1 2 3 (4 5 6 (7 8 9))) I would get (((9 8 7) 6 5 4) 3 2 1) or if I had the list(1 2 ...
Just a quick answer, not sure about efficiency/elegancy: (defun reverse-deeply (list) (mapcar #'(lambda (li) (cond ((consp li) (reverse-deeply li)) (t li))) (reverse list)))
4,374,530
4,383,580
How do I delete from a binary search tree in Lisp
How can I delete a node from a BST? I need an algorithm to do that in Dr. Scheme.
You basically toss the BST you have now, and create a new one sans the element. You can do this by recursively descending the tree. If your item is less than the root datum, create a BST whose root and greater-than branch is copied from what you have now, but whose less-than branch is the result from a recursive call. ...
4,387,967
4,388,300
Does a setfable nthcdr implementation exist?
I am using clisp and I wonder if there is any library with a setfable version of nthcdr that I can use.
You can hack around it with: (let ((lst (list 1 2 3 4)) (n 2)) (setf (cdr (nthcdr (1- n) lst)) '(5 6 7)) l) > (1 2 5 6 7) Or define your own setf for it: ;; !!warning!! only an example, lots of nasty edge cases (defsetf nthcdr (n lst) (new-val) `(setf (cdr (nthcdr (1- ,n) ,lst)) ,new-val)) I do not kno...
4,389,440
4,390,382
DCG in Prolog — strings
I'm writing a Lisp-to-C translator using Prolog's built-in DCG capabilities. This is how I handle arithmetic: expr(Z) --> "(", "+", spaces, expr(M), spaces, expr(N), ")", {swritef(Z, "%d + %d", [M, N])}. expr(Z) --> "(", "-", spaces, expr(M), spaces, expr(N), ")", {swritef(Z, "%d - %d", [M, N])}. expr(Z) --> "(", "*", ...
The problem is that the %s format specifier needs the argument to be a list of characters. So you can do it with something like this: :-set_prolog_flag(double_quotes, codes). % This is for SWI 7+ to revert to the prior interpretation of quoted strings. expr(Z) --> "(", "+", spaces, lexpr(M), spaces, lexpr(N), ")", {s...
4,404,662
4,404,727
Lisp: Inspect function determine its required parameters
In Python, I can do this: >>> def foo(x,y,z=1): return x+y*z >>> foo.func_code.co_varnames ('x', 'y', 'z') >>> foo.func_defaults (1,) And from it, know how many parameters I must have in order to call foo(). How can I do this in Common Lisp?
Most implementations provide a way of doing this, but none is standardized. If you absolutely need it, Swank (the Common Lisp part of SLIME) has a function called swank-backend:arglist that, as far as I can see, does what you want: CCL> (swank-backend:arglist 'if) (TEST TRUE &OPTIONAL FALSE) CCL> (swank-backend:arglis...
4,419,544
4,425,408
emacs setup for both clojure and common lisp with slime-fancy (slime-autodoc)
I set up emacs for both clojure and common lisp, but I want also (slime-setup '(slime-fancy)) for common lisp. If I add that line to init.el, clojure won't work: it gives me repl, but it hangs after I run any code. My configuration For clojure: I set up clojure-mode, slime, slime-repl via ELPA I run $ lein swank in pr...
Here is a solution. (using hooks) That is ugly but quite convenient. (add-hook 'slime-connected-hook (lambda () (if (string= (slime-lisp-implementation-type) "Clojure") (setq slime-use-autodoc-mode nil) (setq slime-use-autodoc-mode t)) )) (add-hook 'slime...
4,422,390
4,422,416
Does the DrRacket interpreter use normal-order evaluation based on SICP Exercise 1.5?
One must decide, based on the value of: (test 0 (p)) where test is defined as : (define (test x y) (if (= x 0) 0 y)) and p is defined as : (define (p) (p)) When I evaluate (test 0 (p)) the interpreter goes into an infinite loop, suggesting that it is evaluating p. This shows normal-order evaluation, b...
This shows normal-order evaluation, because the operands are evaluated before being substituted for parameters Actually you got it the wrong way around. Applicative order is when the operands are evaluated first. Normal-order is when the arguments are substituted into the expression unevaluated. So racket uses applic...
4,425,400
56,549,105
Is there a command to halt the interpreter in Common Lisp?
I'm looking for an expression that will cause the interpreter to exit when it is evaluated. I've found lots of implementation-specific ones but none in the HyperSpec, and I was wondering if there were any that I wasn't seeing defined in the specification. I've found that (quit) is recognized by both CLISP and SLIME, an...
There is an ASDF library called shut-it-down that provides a quit function that works by just having cases for the common CL implementations.
4,427,321
4,428,180
Setting up a equal function in common lisp using only "eq"
I've given the assingment to write a function in common lisp to compare two lists to see if they are equal and I have been bared from using the "equal" predicate I can only use "eq" and I seem to come to a wall. I get this error with my code EVAL: variable SETF has no value The following restarts are available: and he ...
I guess this is what you are looking for: (defun compare-lists (list1 list2) (if (and (not (null list1)) (not (null list2))) (let ((a (car list1)) (b (car list2))) (cond ((and (listp a) (listp b)) (and (compare-lists a b) (compare-lists (cdr list1) (cdr lis...
4,436,351
4,455,585
scheme for object-oriented programmers
I'm thoroughly intrigued by Scheme, and have started with some toy programming examples, and am reading through Paul Graham's On Lisp. One thing I haven't been able to find is a book or website intended to teach Scheme to "OO people", i.e. people like myself who've done 99 % of their coding in c++/Java/Python. I see t...
I doubt CLOS would serve as a crutch for old habits, I found it to be pretty different from the OO style in C++/Java/Python, and very interesting. I don't understand all the details, but I would recommend Peter Seibel's Practical Common Lisp. If you are reading On Lisp without much trouble, you should be able to dive i...
4,439,326
4,440,453
Position of All Matching Elements in List
I'm trying to write a function in Common Lisp similar to the built in position function, that returns a list of the positions of all elements in the haystack that match the needle, as opposed to just the first. I've come up with a few possible solutions (for example recursively searching for the next element using a cd...
The obvious way to solve the problem is just to look at each element of the list in turn, and each time one compares as equal to the needle collect its position into an output list. Getting the position is very easy in this case, because we are starting from the beginning of haystack; we can use a variable to count the...
4,443,518
4,444,695
Are there any fairly mature Lisp/Scheme/Clojure compilers for .Net CLR?
I am seeing several variants out there; ClojureCLR, LSharp, IronScheme, IronLisp, among others. Are any of these actively maintained and/or anywhere close to "mature", or are they mostly experiments or dust-gatherers? Which would be considered the most mature framework for compiling to .Net dll's and referencing othe...
IronLisp is dead and superseded by IronScheme, which in turn is still beta. L Sharp and ClojureCLR are similar and they follow same idea of modern Lisp for CLR (in contrast to IronScheme, which tries to just implement the R6RS standard on the new platform). ClojureCLR seems to be more popular than L Sharp, and Java's ...
4,460,082
4,465,396
print list of symbols in clojure
I was trying to print out a list of symbols and I was wondering if I could remove the quotes. (def process-print-list (fn [a-list] (cond (empty? a-list) 'false (list? a-list) (let [a a-list] (println (first a)) (process-print- list (rest a) )) :else (process-print-list (rest a-list) )))) the list is ('x 'y 'z)...
You should use name fn to get symbol name. (def my-list (list 'x 'y 'z)) (defn process-list [a-list] (map #(name %) a-list)) (process-list my-list) ;=> ("x" "y" "z") Or with printing (defn process-print-list [a-list] (doall (map #(println (name %)) a-list)) nil) (process-print-list my-list) ;...
4,460,772
4,460,863
Format in cl-who does't work properly
I'm trying to build a personal Website via hunchentoot and cl-who, but I'm occurring an semantic error in the following code: (defun index () (standart-page (:title "~apb") (dolist (article (articles)) (cl-who:htm (:ul (:li (format nil "~a: ...
Looking at the examples of usage on the CL-WHO site I don't think you can just do a format that returns a string. All of their examples either use custom output functions (for example fmt) that appear to write to a dynamic variable under the hood. In the example of code generated by CL-WHO you see this as a (format h...
4,470,211
4,470,614
Setting List Values to Numbers in CL, and Subsequently Checking Them
I'm playing around in CL, making a One-Dimensional version of Battleship before I try to tackle a full Two-Dimensional version, and I've hit a hangup. To check if the boat is there, I've represented it with zeroes, and when a spot is hit, I replace it with an asterisk, so I can check the list with numberp. However, whe...
You are not entering zeroes at all, but rather the letter 'O'. Other notes: Do not use DEFPARAMETER inside DEFUN. Define the variable at top level, and inside the initialization function just SETF it. Do not use lists for random access. Use arrays. Numerical comparison operators will signal an error when given a non-nu...
4,474,583
4,479,830
Help writing emacs lisp for emacs etags search
I'm looking for some help developing what I think should be an easy program. I want something similar to Emacs tags-search command, but I want to collect all search results into a buffer. (I want to see all results of M-,) I'm thinking this python style pseudo code should work, but I have no idea how to do this in emac...
Since I'm such a fan of igrep, I'd use it as the building block. From there it's two simple routines and you're done. With that library and these two functions, all you have to do is: M-x igrep-tags ^SomeRegexp.*Here RET Here's the code: (require 'igrep) (defun igrep-tags (regex) (interactive "sTAGS Regexp: ") (...
4,479,471
4,479,973
Dynamic variables in Lisp Case statement
I wrote this piece of code in common lisp (ignore the ... as it is pointless to paste that part here). (case turn (*red-player* ...) (*black-player* ...) (otherwise ...)) red-player and black-player are variables that were defined using defvar statement, in order to "simulate" a #define statement in C. (defv...
You can abuse Lisp in any way you like. It is flexible like that, unlike C. It doesn't always like the uses you put it to. Why push Lisp around? Try this approach: (defvar *turn* nil) (cond ((eq *turn* 'red) ... (setq *turn* 'black))) ((eq *turn* 'black) ... (setq *turn*...
4,480,994
4,482,546
An efficient collect function in Common Lisp
I'm learning Lisp and have written the following function to collect a list of results. (defun collect (func args num) (if (= 0 num) () (cons (apply func args) (collect func args (- num 1))))) It produced similar output to the built in loop function. CL-USER> (collect #'random '(5) 10) (4 0 3 0 1...
Common Lisp implementations are not required by the ANSI standard to do tail call optimization; however, most that worth their salt (including SBCL) do optimize. Your function, on the other hand, is not tail recursive. It can be turned into one by using the common trick of introducing an extra parameter for accumulat...
4,484,441
4,484,585
"Invalid EXCL::PREDICATE argument" error in Common Lisp
I'm making a classroom excercise in LISP, and I'm getting this error CG-USER(286): Error: Invalid EXCL::PREDICATE argument: #<Vector @ #x20fd488a> [condition type: SIMPLE-ERROR] Could you tell me what this is supposed to mean? I'll paste the code giving the error, but it's long and ugly. It should find the st...
You would need to paste a backtrace. But as I read it, the error basically says that where Lisp expected a predicate, it got some kind vector data. Typically this would be because some arguments are in the wrong position.
4,484,595
4,484,608
Use the elements of the list in a format function
I want to do something like: (setf list '(1 2 3 4 5 6)) (format t "~A some text here ~A ~A ~A more text here ~A ~A" list) And have the output be 1 some text here 2 3 4 more text here 5 6 How can I do this without calling (nth 1 list) (nth 2 list) etc?
Try (apply #'format t "~A some text here ~A ~A ~A more text here ~A ~A" list)
4,497,073
4,497,165
Elisp: Asking yes-or-no in interactive commands
I'm new to Emacs and am trying to write a few Emacs Lisp functions. I'd like to write a function that takes two parameters and can handle being interactive. However, one of the parameters is a boolean — it'd be perfect if I could use (y-or-no-p), but (interactive) doesn't seem to have a character code for that. Any ide...
Ah, found it. (defun foo (str bool) (interactive (list (read-string "Some text: ") (y-or-n-p "Do the thing? "))) (some-func str) (if bool (some-other-func str)))
4,504,529
4,504,856
lisp defclass macro problem
Bit of background, I'm a total lisp noob, only started a few weeks ago, but I've been developing in other langs for years. Logic no problem, lisp, problem. I'm trying to write a macro that will define two clsql classes for me to get around a problem with the library. I'd like the classes to be named x and `x-insert`` ,...
It is difficult to begin an explanation here, since you seem to have a whole stack of misconceptions. First question (how to compose symbol names): Lisp macros do not operate on text but on code. In a backquote form, ,class evaluates to the code passed into the class parameter of the macro, most likely a class name in...
4,537,793
4,538,245
What are the tasks of the "reader" during Lisp interpretation?
I'm wondering about the purpose, or perhaps more correctly, the tasks of the "reader" during interpretation/compilation of Lisp programs. From the pre-question-research I've just done, it seems to me that a reader (particular to Clojure in this case) can be thought of as a "syntactic preprocessor". It's main duties are...
Generally the reader in Lisp reads s-expressions and returns data structures. READ is an I/O operation: Input is a stream of characters and output is Lisp data. The printer does the opposite: it takes Lisp data and outputs those as a stream of characters. Thus it can also print Lisp data to external s-expressions. Note...
4,551,283
4,555,421
What is wrong with my emacs/slime setup (compile-and-load/eval not working)?
I can run emacs and start slime (with M-x slime). At this point I get the REPL in the inferior-lisp buffer and can run lisp there. But when I open up lisp code in another buffer none of the slime-goodness works (C-x C-e, C-c C-k etc.) and I keep seeing this in the Messages buffer (with an increasing count-number): sli...
The following is what I did to get Common Lisp and Clojure to work in the same Emacs installation, along with the excellent emacs-starter-kit. This won't let you use both at the same time (you have to restart Emacs to switch from CL to Clojure or vice versa) I believe that the version of SLIME in ELPA is old, but works...
4,558,113
4,558,476
Simplest way to get a Lisp "hello world" web application going
I want to write a Lisp web application just for fun. I found this question about Lisp web apps but all the answers seem so complicated. After looking into the links provided in the answers, the solutions seem really complicated. If I just want a simple, "hello world" Lisp web app, is there not a simple way to do it?
This answer LOOKS complicated, but I think that getting a simple Lisp web app up and running is going to be easier than learning the other more awesome bits of Lisp anyway, so it's probably worth it. There's a couple of really great Common Lisp books with intro-to-web-app chapters: Practical Common Lisp and Land of Lis...
4,559,121
4,559,413
Installing a condition handler in Common Lisp
The HTTP library Drakma on CLISP generates an error USOCKET:UNSUPPORTED due to a bug in Drakma+CLISP. However, it turns out that the CONTINUE restart seems to work fine. Therefore, I spent some time with CLtL and other references trying to determine how to write a restart handler. (defun http-request (url param) (han...
Well, I am not sure if I can help you here, but: Your parens are totally messed up. Try it like this: (defun http-request (url param) (handler-bind ((usocket:unsupported #'continue)) (drakma:http-request url :method :post :parameters param))) If that doesn't work, try checking whether you are really handling the...
4,559,971
4,606,068
Mapping variable argument LISP function to C function - C
I am developing a custom LISP interpreter. It won't support defining functions like in LISP, instead all functions are mapped to C functions. When it sees an expression like, (substr 'input '1 '1) it knows to call internal substr function and return the result. Now I am planning to implement a message function which ...
I doubt there is a way to do this. The reason is that the number of parameters to your lisp function is only known at runtime, but the number of arguments to a C function must be known at compile time. This includes va_lists unless you want to hack at them in some kind of platform specific way. The best you can reall...
4,565,325
4,565,779
Too many arguments for function
I'm starting to learn Lisp with a Java background. In SICP's exercise there are many tasks where students should create abstract functions with many parameters, like (define (filtered-accumulate combiner null-value term a next b filter)...) in exercise 1.33. In Java (language with safe, static typing discipline) - a...
SICP uses a subset of Scheme SICP is a book used in introductory computer science course. While it explains some advanced concepts, it uses a very tiny language, a subset of the Scheme language and a sub-subset of any real world Scheme or Lisp a typical implementation provides. Students using SICP are supposed to start...
4,568,861
4,570,326
Common lisp, CFFI, and instantiating c structs
I've been on google for about, oh, 3 hours looking for a solution to this "problem." I'm trying to figure out how to instantiate a C structure in lisp using CFFI. I have a struct in c: struct cpVect{cpFloat x,y;} Simple right? I have auto-generated CFFI bindings (swig, I think) to this struct: (cffi:defcstruct #.(chip...
Most Common Lisp implementations do not allow passing structures on stack. There is a fsbv library which uses libffi to add that capability. If you know the structure layout you can decompose it manually as a series of basic arguments, but that is obviously brittle.
4,571,119
4,571,301
Ruby or Python for heavy import script?
I have an application I wrote in PHP (on symfony) that imports large CSV files (up to 100,000 lines). It has a real memory usage problem. Once it gets through about 15,000 rows, it grinds to a halt. I know there are measures I could take within PHP but I'm kind of done with PHP, anyway. If I wanted to write an app that...
What are you importing the CSV file into? Couldn't you parse the CSV file in a way that doesn't load the whole thing into memory at once (i.e. work with one line at a time)? If so, then you can use Python's standard csv library to do something like the following import csv with open('csvfile.csv', 'rb') as source: ...
4,572,608
4,573,125
Porting a piece of Lisp code to Clojure (PAIP)
I'm reading Paradigms of Artificial Intelligence Programming (PAIP) by Peter Norvig and I'm trying to write all the code in Clojure rather than common Lisp. However I'm stuck on this piece of code on page 39: (defparameter *simple-grammar* '((sentence -> (noun-phrase verb-phrase)) (noun-phrase -> (Article Noun)) ...
I'm a relative Clojure newbie that went through this exact exercise a while back. Something to consider here is whether you'd like to adhere as closely as possible to Norvig's code (like writing "Common-Lisp-flavored" Clojure) or if you'd like to write something closer to idiomatic Clojure. Here's what I did: (use '[...
4,576,591
4,576,700
Land of Lisp example redundency?
I've read a lot of good things about Land of Lisp so I thought that I might go through it to see what there was to see. (defun tweak-text (lst caps lit) (when lst (let ((item (car lst)) (rest (cdr lst))) (cond ; If item = space, then call recursively starting with ret ; Then, prepend th...
Indeed, you are correct. See the errata for the book. Page 97: The function tweak-text has two glitches in it, though it will run OK on most Lisp implementations. First of all, it uses the eq function to compare characters- Characters should always be checked with other functions such as eql or char-equal as per the ...
4,578,574
4,578,888
What is the difference between Lisp-1 and Lisp-2?
I have tried to understand the difference between Lisp-1 and Lisp-2 and how this relates to Clojure but I still do not understand properly. Can anyone enlighten me?
According to wikipedia: Whether a separate namespace for functions is an advantage is a source of contention in the Lisp community. It is usually referred to as the Lisp-1 vs. Lisp-2 debate. Lisp-1 refers to Scheme's model and Lisp-2 refers to Common Lisp's model. It's basically about whether variables and functions ...
4,587,918
4,588,525
Comparing Common Lisp with Gambit w.r.t their library access and object systems
I'm pretty intrigued by Gambit Scheme, in particular by its wide range of supported platforms, and its ability to put C code right in your Scheme source when needed. That said, it is a Scheme, which has fewer "batteries included" as compared to Common Lisp. Some people like coding lots of things from scratch, (a.k.a. v...
1) I haven't used Gambit Scheme, so I cannot really tell how smooth the C/C++ integration is. But all Common Lisps I have used have fully functional C FFI:s. So the availability of C libraries is the same. It takes some work to integrate, but I assume this is the case with Gambit Scheme as well. After all, Lisp and C a...
4,589,366
4,589,422
The most minimal LISP?
Possible Duplicate: How many primitives does it take to build a LISP machine? Ten, seven or five? I am curious. What is the most minimal LISP, upon which all further features could be built? Ignore efficiency -- the question comes simply from a place of elegance. If you awoke on an alien planet and were instructed...
Courtesy of Paul Graham, here's a Common Lisp implementation of John McCarthy's original LISP: It assumes quote, atom, eq, cons, car, cdr, and cond, and defines null, and, not, append, list, pair, assoc, eval, evcon and evlis.
4,597,167
4,638,602
Tierless web framework with Javascript?
Links is a lisp-like functional web programming language/framework that makes it easy to write a single piece of code that is compiled to server-side code, client-side JS and HTML, thus making it much easier to write web applications. Since there really is no distinction between the client and server side, they call it...
I've read a little about Jaxer: http://jaxer.org
4,606,304
4,606,763
sorting lists according to some elements
I am a newbie in Lisp and I want to learn Lisp programming. I want to sort some lists read from a text file, like in the following form: (a 120 135 124 124) (b 120 135 124 124) (c 120 135 124 124) What is the best way to sort them according to the first integer element or maybe second or third and so on? I have the f...
The standard sort function takes a :key argument that can be used to extract a value from the object to use as the sort key. For your example, if you had each list from the file in a list called objects, the following would destructively sort objects by the first integer element and return a sorted list: (sort objects ...
4,617,904
4,618,566
What is a good platform for building a game framework targeting both web and native languages?
I would like to develop (or find, if one is already in development) a framework with support for accelerated graphics and sound built on a system flexible enough to compile to the following: native ppc/x86/x86_64/arm binaries or a language which compiles to them JavaScript ActionScript bytecode or a language which com...
There is a language currently in development by Blitz Research ( http://www.blitzbasic.com ), it is NOT YET released, although the release date looms close. The blog of the main developer is here: http://marksibly.blogspot.com/2010/05/hi-ok-heres-plan-i-am-currently-working.html This language has been announced as free...
4,618,503
4,619,402
format - Help with printing a table
This question will probably end in a facepalm, but I've tried for a while and am still stuck despite reading through the hyperspec. Basically what I want to do is something like (format t "~{|~{ ~5d~}|~%~}" '((1 23 2 312) (23 456 1 7890))) but instead of hard-coding the 5 it should be calculated from the list (length ...
Assuming the required width is bound to width, then you can do this: (format t "~{|~{ ~Vd~}|~%~}" width '((1 23 2 312) (23 456 1 7890))) 5 has been replaced by V and width has been added as an argument to FORMAT/ edit: original answer did not correctly account for the nested directives In a format control string V may...