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
74,165,987
74,166,696
Lexing Lisp in Python
I have been interested by the Lisp language, and I decided to create my own dialect. This is going to be the most simple one to ever exist. As you know, everything in Lisp is a list (or at least this dialect). A list consists of a command that comes at the start of it, and maybe arguments which are lists themselves. Us...
A simple Lisp parser can be easy to implement, you can basically write a recursive descent top-down parser. You have individual readers, like readers for integers, strings, symbols, whatever you want: class Reader: def read_integer(self, stream): pass def read_string(self, stream): pass d...
74,329,238
74,347,760
Return values with indexes from list in AutoLisp
I am trying to create a new list by taking values from the user. Then I want to display the values in this list along with their indexes. For example for the values 5,4,3,2 I want the output to be: 5 0 4 1 3 2 2 3 This is my code: (setq n (getint "How many elements you want to input")) (defun CreateList(n) (setq lis...
Firstly, a few comments about your code: (defun CreateList(n) Always declare your local variables as part of the defun expression - this will limit the scope of the variables to the function in which they are declared. Without limiting the scope of the variable, its value will be retained after the function has comple...
74,363,520
74,363,714
I wish to change the format of the numbers contained in a list Common Lisp Allegro
I have lists which contain numbers with too many decimals. I would like to recondition the list changing the format of the list elements. For instance I have '( 1.233453923729 3.44566546 9.1111111) and I would like to keep max two decimals. I tried with a dolist to extract the list component to write them on a stream ...
You can multiply your float by a power of 10, call FROUND and divide by the same factor: (defun round-float (float places) (check-type places (integer 1)) (let ((factor (expt 10 places))) (/ (fround (* float factor)) factor))) Then you can apply the same function to a...
74,427,834
74,429,254
Why is a literal quoted list not the same as a list built by list constructors
(equal? '(1 2) (cons 1 2)) ; #f Why exactly are these two not equivalent considering lists are just that — illusion built on top of cons cells.
(cons 1 2) does not create a proper list. It creates a cons cell with two elements, both are numbers. A normal list is recursively defined as: the empty list or a cons cell with an object as the car and a list as the cdr. In your call 2 is not a list, but a number. The second argument to cons must be a list to get a ...
74,476,556
74,478,559
How do I convert a string to a hash ( key: character, value: list of the index of the character ) in Racket?
I want to convert a string to a hash table, whose keys are the characters in the string, and values are are the list of index of this character in the string. Is there some elegant way to do this? For example: String: "abcaad" Hash table: { a: [0 3 4] , b: [1] , c: [2] , d: [5] } I tried to write one with hash-set! a...
We can simplify the solution a bit by using in-indexed: ; procedural programming (define (s->hash s) (define ht (make-hash)) (for (((c i) (in-indexed s))) (hash-set! ht c (cons i (hash-ref ht c empty)))) ht) Or we can write a functional programming style solution by composing existing built-in procedures and...
74,483,828
74,488,497
AutoLisp DCL - Multi-line Text with Button
I'm trying to reduce the width of a button by making the text inside the button two lines instead of one. I thought using "\n" would produce a new line within the button, but it only added a comma. Is there a way to add more than one line of text within a button, or is this just a limitation of the language? Failed new...
As far as I'm aware, multiline text on buttons is not possible using standard DCL - you would need to turn to OpenDCL or similar.
74,529,969
74,532,190
Is it necessary for `progn' to be "special operator"?
If progn is NOT a special operator, it could be defined as macro like this: (defmacro progn (&rest body) `(let () ,@body)) I guess that defmacro's definition may have used some progns, but it only needs to substitute (let () ...)s for those (progn ...)s. Less special operators, more elegant. Am I wrong?
[This answer concerns Common Lisp, not Lisp in general.] progn must be special at least because it must be handled specially at top-level. In particular when compiling a file containing, say (defun foo ...) (progn (defun bar ...) (progn ...)) Then the forms inside the progns are at top-level and are recognise...
74,533,753
74,534,088
Why exactly is filtering using a set more performant than filtering using a vector?
After some researching, I was recently able to dramatically improve the performance of some code by using a set to compare rather than a vector. Here is a simple example of the initial code: (def target-ids ["a" "b" "c"]) (def maps-to-search-through [{"id": "a" "value": "example"} {"id": "e" "value": "example-2...
Sets are data structures that are designed to only contain unique values. Also you can use them as functions to check whether a given value is a member of the very set - just as you use your target-ids set. It basically boils down to a call of Set.contains on JVM side which uses some clever hash-based logic. Your first...
74,580,351
74,580,645
How can I call a LISP function with &optional arg and colon like `(formato '((:year 0 4) (:month 4 6) (:day 6 8)))`
I am trying to perform a DB migration with pgloader and I would like to define a custom lisp transformation file for a datetime, but I don't know LISP (!) So I have cut&paste one of the pgloader transform functions and I tried to invoke it with no luck after spending a bunch of hours (!) in studying LISP. I have extrac...
You're right, format is a variable here, but Common Lisp is Lisp2, so variables can have the same name as functions and you don't overwrite core function. Optional argument formato is a list, containing triplets (keyword number number)- so if you want to provide your own format, it has to have exactly this form. And yo...
74,587,201
74,589,015
Why does a macro inside a loop only expand once in Elisp?
Let's say I define (defmacro macro-print (str) (print "hello")) and run (let ((i 0)) (while (< i 3) (macro-print i) (setq i (+ 1 i)))) The output is "hello" nil why is this? I was expecting to see 3 hellos. This is because, according to the manual, the while special form first evaluates its condition, if...
Macros are essentially functions whose domain and range is source code. In CL they are quite literally this: a macro is implemented by a function whose argument is source code (and an environment object) and which returns other source code. I think that in elisp the implementation is a little less explicit. As such m...
74,589,723
74,591,527
How to replace an append with a cons in Common Lisp?
How I can replace an append with a cons in my lisp program that reverses a list and all it's substrings? (defun revert (l) (cond ((atom l) l) (t (append (revert (cdr l)) (list (revert (car l))))))) (write (revert '(2 3 5 6 7 8 9 (4 5 (6))))) With append the result is: (((6) 5 4) 9 8 7 6 5 3 ...
As you have seen, you cannot simply replace append with cons, because they are two very different operators: append concatenates two lists in a single list, while cons takes an element and a list and returns a new list with the element in first position, and the rest of the list the second parameter. One very simple wa...
74,609,276
74,611,747
Are Lisp macros just syntactic sugar?
I keep reading that Lisp macros are one of the most powerful features of the language. But reading over the specifications and manuals, they are just functions whose arguments are unevaluated. Given any macro (defmacro example (arg1 ... argN) (body-forms)) I could just write (defun example (arg1 ... argN) ... (body-for...
I can't talk about powerful because it can be a little bit subjective, but macros are regular Lisp functions that work on Lisp data, so they are as expressive as other functions. This isn't the case with templates or generic functions in other languages that rely more on static types and are more restricted (on purpose...