lisp

once-only lisp macro, is my implementation correct?

ⅰ亾dé卋堺 提交于 2019-12-24 04:55:07
问题 I am trying to learn Lisp from Peter Seibel's book "Practical Common Lisp". In chapter 8 : "Macros: Defining your own", I came across this once-only macro. At the bottom of that page, an implementation is given. Now that is a pretty complicated macro to understand for me, so I saw this question on stackoverflow and there are some good explanations there. However, even if I (still) havent fully understood the macro, I understood its purpose. So I tried to write my own implementation of it :

LISP: multi-level recursive reverse function

霸气de小男生 提交于 2019-12-24 04:26:13
问题 How to reverse a list such that every sublist is also reversed? This is what I have so far: (defun REV (L) (cond ((null L) nil) ((listp L) (append (REV (cdr L)) (list (car L)))) (t (append (REV (cdr L)) (list (car L)))))) 回答1: You are on the right track, but your last two conditions have the same action, which should give an indication that one of them is not doing what it should. Indeed, the second condition, the listp case, is not right, because when it's a list, you need to append the

Lisp Infinite Recursion

女生的网名这么多〃 提交于 2019-12-24 02:49:45
问题 I am a new lisp programmer, I am having trouble wrapping my head around recursion in lisp. I have a series of expressions that I simplify by going through a series of methods that replace symbols with numbers and then I will evaluate the expression. Before evaluation I substitute symbols for numbers, in doing that I get a stack overflow error in my subst-bindings method and/or when I call the deep-subst method from within that method. Any help or advice on my recursive method calls that would

Lisp Function that Returns a Sum

半世苍凉 提交于 2019-12-24 02:41:02
问题 I am trying to write a weird function, so bear with me here. This function should take a list L as a parameter and have a sum variable. If L is not a list, it should return nil . Otherwise, it should iterate through each element of the list and do the following: If the element is a number and less than zero, it should subtract 1 from the sum. If the element is a number and greater than zero, it should add 1 to the sum. if the element is 0 or not a number, then it should add 0 to the sum. Here

(Racket/Scheme) Subtraction yields result off by incredibly small margin

喜你入骨 提交于 2019-12-24 02:07:48
问题 I'm currently messing around with "How To Design Programs" - using Scheme/Racket; I've come across a really peculiar feature in the R5RS version of Scheme. When conducting a simple subtraction, albeit using values with decimal point accurace, answers are minutely off what would be expected. For example; given the following subtraction operation: (- 5.00 4.90) => 0.09999999999999965 When one should surely be expecting a clean 0.10? Whole number subtraction works as expected; > (- 5 4) => 1 > (

Racket - implementing the let* function using macro

﹥>﹥吖頭↗ 提交于 2019-12-24 01:54:54
问题 I need to implement my_let* using defmacro which works similarly to let*, but while let* is expanded to a series of nested let calls (behind the scenes), my_let* needs to be expanded to a single let call, and use the define statement to define the arguments i get. an example of using my_let*: (my_let* ((a 2) (b 3) (c (+ a b))) (+ a b c)) and the return value of this code should be 10. just as if it was use let*. the code above will be expanded in my_let* to the following: (let () (define a 2)

duplicating and modifying the head of a list of list, in Lisp

风流意气都作罢 提交于 2019-12-24 01:43:48
问题 I'm learning Lisp. I wish to add a new list to a list of list, say ((1 1 1) (0 0 0)), where the new head of this list collection is computed based on the previous head. Here's what I tried, in the REPL environment in Slimv with sbcl: > (defvar *ll* (list (list 1 1 1) (list 0 0 0))) *LL* > *ll* ((1 1 1) (0 0 0)) > (push (car *ll*) *ll*) ((1 1 1) (1 1 1) (0 0 0)) > (setf (nth 2 (car *ll*)) 2) 2 > *ll* ((1 1 2) (1 1 2) (0 0 0)) As shown above, I only wanted to modify the last element of the

Trim curly braces from string

风流意气都作罢 提交于 2019-12-24 01:24:25
问题 I am using a Prolog query in a Common Lisp program to get the date of birth from a knowledge base. The query returns the value formatted as {1991-05-13} , and I assign to this value on dob variable with setq : (setq dob {1991-05-13}) . I want to use this date value in a new function which takes string, so I am trying to use write-to-string to convert dob to a string with (setq strdob (write-to-string dob)) , but it returns "{1991-05-13}" I actually want: "1991-05-13" which lacks the curly

Can I use lambda with an on-the-fly lambda list (without macros)?

早过忘川 提交于 2019-12-24 01:16:20
问题 I'm trying to create a function to return functions, with arbitrary lambda lists, generated on the fly. I can do it with macros, but I'm trying to de-macro-ify what I've already got: (defmacro make-canned-format-macro (template field-names) `(function (lambda ,field-names (apply #'format `(nil ,,template ,,@field-names))))) I can use it as follows: * (make-canned-format-macro "~A-powered ~A" (fuel device)) #<FUNCTION (LAMBDA (FUEL DEVICE)) {10067D975B}> * (setf (fdefinition 'zoom-zoom) (make

Could an iterative function call itself?

て烟熏妆下的殇ゞ 提交于 2019-12-24 01:01:55
问题 When viewing the below MIT 6.001 course video, at 28:00 the instructor marks this algorithm as iteration. But, at 30.27 he says both this and the actual "recursive" algorithms are recursive. The function is calling itself with a base case, so how is this iteration? https://www.youtube.com/watch?v=dlbMuv-jix8&list=PLE18841CABEA24090&index=2 private int iterativeSum(int x, int y) { System.out.println("x "+x+" y "+y); if(x == 0) { return y; } return iterativeSum(--x, ++y); } 回答1: He seems to be