lisp

Common Lisp Double-Backquote, Unquote, Quote, Unquote sequence?

旧城冷巷雨未停 提交于 2019-12-03 05:47:16
问题 I'm reading Let Over Lambda, which deals with some pretty deeply layered macro authoring. It's fascinating and I'm mostly managing to keep up with it. In Chapter 4 Hoyte implements reader macros for CL-PPCRE match and replace functions, such that you can do things like: (#~m/(foo|bar)\d+/ "Some foo99") ; matches! (#~s/foo(\d+)/bar\1/, "Some foo99") ; "Some bar99 In order to achieve this, we define a macro that uses the double-backquote, since it is actually expanded by a wrapper macro, which

converting number to string in lisp

自闭症网瘾萝莉.ら 提交于 2019-12-03 05:47:11
问题 I tried to find a lisp function to convert between numbers and strings and after a little googling I fond a function with the same name. when I entered (itoa 1) SLIME printed: Undefined function ITOA called with arguments (1) . How can I do the conversion? 回答1: From number to string: (write-to-string 5) "5" you may transform a string to any numerical notation: (write-to-string 341 :base 10) "341" From string to number: (parse-integer "5") 5 with some trash (parse-integer " 5 something not a

Compose example in Paul Graham's ANSI Common Lisp

百般思念 提交于 2019-12-03 05:41:36
Can anybody explain an example in Paul Graham's ANSI Common Lisp page 110? The example try to explain the use &rest and lambda to create functional programming facilities. One of them is a function to compose functional arguments. I cannot find anything explaining how it worked. The code is as follows: (defun compose (&rest fns) (destructuring-bind (fn1 . rest) (reverse fns) #'(lambda (&rest args) (reduce #'(lambda (v f) (funcall f v)) rest :initial-value (apply fn1 args))))) The usage is: (mapcar (compose #'list #'round #'sqrt) '(4 9 16 25)) The output is: ((2) (3) (4) (5)) Line 2 and 6 look

Elisp: How to delete an element from an association list with string key

血红的双手。 提交于 2019-12-03 05:12:37
Now this works just fine: (setq al '((a . "1") (b . "2"))) (assq-delete-all 'a al) But I'm using strings as keys in my app: (setq al '(("a" . "foo") ("b" . "bar"))) And this fails to do anything: (assq-delete-all "a" al) I think that's because the string object instance is different (?) So how should I delete an element with a string key from an association list? Or should I give up and use symbols as keys instead, and convert them to strings when needed? If you know there can only be a single matching entry in your list, you can also use the following form: (setq al (delq (assoc <string> al)

Lisp as a Scripting Language in a C++ app [closed]

血红的双手。 提交于 2019-12-03 05:09:07
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . Hey, I've been looking at the possibility of adding a scripting language into my framework and I heard about Lisp and thought I would

How to map clojure code to and from JSON?

末鹿安然 提交于 2019-12-03 04:53:56
问题 I have a crazy idea, which involves putting some clojure code into CouchDB and writing views that query it. I don't want to store the clojure code as plain text, because then I would have to worry about parsing it in the views. Formatting and comments don't need to be preserved, but the code should be able to go in and out of the database without changing in structure. Keywords, symbols, and strings should all stay in their native type. Additionally, I want the code to look elegant and be

Help me write a Clojure macro which automatically adds metadata to a function definition

人走茶凉 提交于 2019-12-03 04:52:51
I realize that the first rule of Macro Club is Don't Use Macros, so the following question is intended more as an exercise in learning Clojure than anything else (I realize this isn't necessarily the best use of macros). I want to write a simple macro which acts as a wrapper around a regular (defn) macro and winds up adding some metadata to the defined function. So I'd like to have something like this: (defn-plus f [x] (inc x)) ...expand out to something like this: (defn #^{:special-metadata :fixed-value} f [x] (inc x)) In principle this doesn't seem that hard to me, but I'm having trouble

What does “my other car is a cdr” mean?

≯℡__Kan透↙ 提交于 2019-12-03 04:39:13
问题 Can anyone well versed in lisp explain this joke to me? I've done some reading on functional programming languages and know that CAR/CDR mean Contents of Address/Decrement Register but I still don't really understand the humour. 回答1: In Lisp, a linked list element is called a CONS. It is a data structure with two elements, called the CAR and the CDR for historical reasons. (Some Common Lisp programmers prefer to refer to them using the FIRST and REST functions, while others like CAR and CDR

Is there an equivalent to Lisp's “runtime” primitive in Scheme?

拈花ヽ惹草 提交于 2019-12-03 04:36:40
According to SICP section 1.2.6 , exercise 1.22: Most Lisp implementations include a primitive called runtime that returns an integer that specifies the amount of time the system has been running (measured, for example, in microseconds). I'm using DrScheme , where runtime doesn't seem to be available, so I'm looking for a good substitute. I found in the PLT-Scheme Reference that there is a current-milliseconds primitive. Does anyone know if there's a timer in Scheme with better resolution? current-milliseconds is a function that returns the current millisecond count from the system, but it

How to do Pattern Matching in Common Lisp

走远了吗. 提交于 2019-12-03 04:32:55
问题 I have no idea if there exists a pattern matching function for Common Lisp, nevertheless I have to make my own function. I have no idea about Lisp. Can somebody give heads-up on learning Lisp and most importantly, how to go about doing pattern matching in Lisp. I will have to pass a pattern and a fact and say if they match. An example would be (heroes (hitpoints=hp) (mana=m)) should match (Morphling (hitpoints 435) (mana 260)) it should also be able to also do numeric comparisons of if a