common-lisp

Unroll / splat arguments in common lisp

天涯浪子 提交于 2021-02-04 17:13:30
问题 Say I have a list of arguments: > (setf format-args `(t "it's ~a" 1)) (T "it's ~a" 1) How can I then "splat" or "unroll" this into a series of arguments rather than a single list argument, for supplying to the format function? i.e I would like the following function call to take place: > (format t "it's ~a" 1) For reference, I would write the following in python or ruby: format(*format-args) I'm sure it can be done, but perhaps I'm thinking about it wrong. It also doesn't help that the name

Defining a “minimum” function to return the minimum of a list using another function that returns the smaller of two numbers

这一生的挚爱 提交于 2021-01-29 03:20:35
问题 (defun *smaller* (x y) ( if (> x y) y x)) (defun *minimum* (lst) (do ((numbers lst (cdr numbers)) (result (car numbers) (*smaller* result (car numbers)))) ((null numbers) result))) LISP says that variable "numbers" in "minimum" function is an unbound one although I think I've bound it to "lst". What am I missing? 回答1: Do binds in parallel. The expression for the initial value of result is (cdr numbers) , and numbers is unbound there. Do* would work here. 回答2: Another approach to this problem

Generate TYPECASE with macro in Common Lisp

家住魔仙堡 提交于 2021-01-28 10:51:58
问题 I have a list of two element sublists which will change and grow in the course of the program. I want to write a macro which takes a key and generates a case dynamically like: ;; This is the List for saving CASE clauses (setf l '((number 2) (symbol 3))) ;; and i want to have the following expansion (typecase 'y (number 2) (symbol 3)) I could have a macro which only refers to the global l : (defmacro m (x) `(typecase ,x ,@l)) which would expand correctly (m 'y) ;expands to (TYPECASE 'Y (number

problems with lisp and do construct with itterating

断了今生、忘了曾经 提交于 2021-01-28 07:58:05
问题 Firstly, I am having this problem. The code I generated isn't iterating thru each word, but instead thru the entire passed argument. I am using a do loop to pass information into a hash table. (defun set_isa (partOfSpeech &rest words) (do ((wordVar words)) ((null wordVar) nil) (putp partOfSpeech word-dict wordVar) (setf wordVar (cdr wordVar)))) With this I am getting this as a result using trace (set_isa 'Verb 'Talk 'Run 'jump ) 1. Trace: (SET_ISA 'VERB 'TALK 'RUN 'JUMP) 1. Trace: SET_ISA ==>

Lisp: Accessing recursive hashes with syntactic sugar

蓝咒 提交于 2021-01-28 00:19:20
问题 I'm trying to build a function (or macro) to ease the getting and setting of data deep in a hash table (meaning, a hash within a hash, within a hash, etc). I don't think I can do it with a macro, and I'm not sure how to do it with eval. I'd like to be able to do the following: (gethashdeep *HEROES* "Avengers" "Retired" "Tony Stark") and have that return "Iron Man" The hashes are all created with: (setf hashtablename (make-hash-table :test 'equal)) and populated from there. I can do the

How do I create a jar using armed bear common lisp?

妖精的绣舞 提交于 2021-01-28 00:09:28
问题 I was wondering if it's possible to create a jar file using armed bear common lisp and if so how to do it. So in other words, I have this following code (format t "Hello, World!~%")) I can run it in armed bear common lisp. I was wondering how I could create a jar that executes that code.– Thanks, Shaun. 回答1: The documentation says (in section 3.3, sorry for the pdf) - 3.3.2 Implemented JSR-223 interfaces JSR-223 defines three main interfaces, of which two ( Invocable and Compilable ) are

How do i access the :Documentation string of a slot of a Defclass in Common lisp

孤街醉人 提交于 2021-01-27 21:33:51
问题 Ok here is How i instantiate my Defclass and related Defmethod and Defparameter (defvar *account-numbers* 0) (defclass bank-account () ((customer-name :initarg :customer-name :initform (error "Must supply a customer name.") :accessor customer-name :documentation "Customer's name") (balance :initarg :balance :initform 0 :reader balance :documentation "Current account balance") (account-number :initform (incf *account-numbers*) :reader account-number :documentation "Account number, unique

Redefinition of the print-object method for conses has different effects in different CL implementations

吃可爱长大的小学妹 提交于 2021-01-27 19:59:46
问题 Trying to print conses not in standard list notation, but always as dotted pairs, with the minimum effort, I have redefined the method print-object in this way: (defmethod print-object((c cons) str) (format str "(~a . ~a)" (car c) (cdr c))) but the effect is different for different implementations. In Clozure CL and in LispWorks Personal the result is what I was expecting: CL-USER 1 > (defmethod print-object((c cons) str) (format str "(~a . ~a)" (car c) (cdr c))) #<STANDARD-METHOD PRINT

Common Lisp macro let-curry - not working

谁说我不能喝 提交于 2021-01-27 18:35:33
问题 I found myself calling lots of methods whose first argument is a complex object from a given class. Whilst with-slots and with-accessors are useful, generic methods cannot be bound in this way. So I thought: if we could locally curry any functions, slots + accessors + generic functions + functions could all be addressed with the same construct. Example of code I want to clean up: (defun clox-string (scanner) "Parse string into a token and add it to tokens" (loop while (and (char/= #\" (peek

Arrays vs. lists in Lisp: Why are lists so much faster in the code below?

自古美人都是妖i 提交于 2021-01-27 05:50:50
问题 I got an unexpected result while solving Problem 75 in Project Euler. My code does find the correct solution, but it behaves strangely. My solution consists of traversing a Pythagorean tree (Barning's matrices) until the perimeter limit is reached, counting the numbers of times the perimeter assumed each value, and, lastly, counting the perimeter lengths that occurred only once. My admittedly untidy but functioning code is: (defparameter *barning-matrixes* '(#(1 -2 2) #(2 -1 2) #(2 -2 3) #(1