lisp

How to order my accumulate variable in this case on Racket?

北城以北 提交于 2019-12-02 04:47:27
I am coding with Racket for educational reasons. I was given a task in which I should create a function that, without filter, would receive a list as an input and return another list only with the even numbers of the first list. I came up with this recursive definition of an iterative process: (define (add-even lista) (define (iter lista accu) (cond ((null? lista) accu) ((even? (car lista)) (iter (cdr lista) (cons (car lista) accu))) (else (iter (cdr lista) accu)))) (iter lista empty)) It works fine. However, I get the result in a reverse order, e.g.: (add-even '(1 2 3 4 5 6 7)) >> '(6 4 2)

Using reduce over a tree in Lisp

北战南征 提交于 2019-12-02 04:44:18
To fold a flat list in Lisp you use reduce : * (reduce #'+ '(1 2 3 4 5)) 15 But what if I have an arbitrarily complex tree, and I want to apply a function over each of the element? So that fold over '(1 (2) (3 (4) 5)) would still give 15 ? I tried to use reduce , but I had to provide a custom function, which kinda defeats the purpose: (defun tree+ (a b) (cond ((null b) 0) ((atom b) (+ a b)) (t (+ (tree+ a (car b)) (tree+ 0 (cdr b)))))) (reduce #'tree+ '(1 (2) (3 (4) 5)) :initial-value 0) ; returns 15 Of course I could flatten the list first, but reduce is a general function, sometimes you must

Am I missing some important fact about interning symbols in LISP?

烂漫一生 提交于 2019-12-02 04:04:55
问题 To be brief. Here is my several tries to intern and use a symbol in clisp. [1]> (setq sym (intern "foo")) |foo| [2]> (eq sym 'foo) NIL Why??? [3]> (defun internup (me &optional (package *package*)) (intern (string-upcase me) package)) INTERNUP [4]> (eq 'abc (internup "abc")) T Probably must be upcase. [12]>(let ((abc 2)) (eval '(+ 2 abc))) *** - EVAL: variable ABC has no value The following restarts are available: ok [18]> (let ((abc 2)) (eval '(+ 2 'abc))) *** - +: ABC is not a number The

Lisp randomize and using two functions to pull from list into another

自闭症网瘾萝莉.ら 提交于 2019-12-02 03:30:33
Okay, so I am new to lisp and i have been working on this program for a couple days getting to know lisp and researching certain parts of lisp such as cons,cdr, let,funcall and some other ones. I am trying to create a candy machine that dispense colors randomly. I have ran this code numerous times and at first it took me a while to stop getting errors for the random function and now it is saying i have too few arguments for the cons in generate-candy-supply. Any one have any suggestions on where to go and any solution to this? so far my code is... (defvar candy-color '(yellow red blue green

How to distribute the asdf/quicklisp dependencies along with an app compiled with Embeddable Common Lisp?

限于喜欢 提交于 2019-12-02 03:00:03
问题 I have tried this example ECL repository asdf example , it works fine but it doesn't have any asdf dependencies. If i add :depends-on (#:inferior-shell) to example.asd then running the compiled standalone executable gives this error : Condition of type: SIMPLE-PACKAGE-ERROR There exists no package with name "ASDF/DRIVER" No restarts available. What causes this error, and what is the idiomatic way of dealing with asdf dependencies on ECL ? 回答1: EDIT: this problem is fixed for ECL newer than 16

How can I generate series of Pell numbers instead of a specific one in Lisp

旧时模样 提交于 2019-12-02 02:48:42
问题 How do I use cons or other way to print a list of Pell numbers till the Nth number? (defun pellse (k) (if (or (zerop k) (= k 1)) k (+ (* 2 (pellse (- k 1))) (pellse (- k 2))))) (print (pellse 7)) 回答1: Here is how to do it in a way that won’t be exponential: (defun pells (n) (loop repeat n for current = 0 then next and next = 1 then (+ (* 2 next) current) collect current)) The time complexity to calculate the n th element given the two previous elements is O(log( P n )) where P n is the n th

Use of # a.k.a. read-macro

南笙酒味 提交于 2019-12-02 02:38:41
Reading book "Let Over Lambda" by Doug Hoyte, I found the following description of #. sign, a.k.a. read-macro: A basic read macro that comes built in with COMMON LISP is the #. read-time eval macro. This read macro lets you embed objects into the forms you read that can't be serialised, but can be created with a bit of lisp code. It's from Chapter 4, most part of the book can be found here: http://letoverlambda.com/index.cl/toc This is example from the book that shows how the same expression may be read differently every time: * '(football-game (game-started-at #.(get-internal-real-time))

How to distribute the asdf/quicklisp dependencies along with an app compiled with Embeddable Common Lisp?

孤街醉人 提交于 2019-12-02 02:36:20
I have tried this example ECL repository asdf example , it works fine but it doesn't have any asdf dependencies. If i add :depends-on (#:inferior-shell) to example.asd then running the compiled standalone executable gives this error : Condition of type: SIMPLE-PACKAGE-ERROR There exists no package with name "ASDF/DRIVER" No restarts available. What causes this error, and what is the idiomatic way of dealing with asdf dependencies on ECL ? EDIT: this problem is fixed for ECL newer than 16.1.3 (fixed in develop branch), so no `require' trick should be needed in the upcoming release. In general

How to display first N natural numbers, knowing the divisors in Lisp

房东的猫 提交于 2019-12-02 02:31:53
Display first N natural numbers, the divisors of which are only 2, 3 and 7. I wrote something like that. I am a beginner in Lisp. Thank you! defvar x 1 (defun numbers(n) if(mod x 2 ) (loop for x from 1 to n do(print x) ) ) print(numbers()) Because I just had some time, you could have a look at this. Might not be the perfect solution but should be a good starting point for a beginner. Check out the books in the info tab to get into the syntax etc. (defun divisible-by (n m) "Returns T if N is evenly divisible by M." (zerop (mod n m))) (defun numbers (n) "Print all number upto N which are

Am I missing some important fact about interning symbols in LISP?

有些话、适合烂在心里 提交于 2019-12-02 02:28:17
To be brief. Here is my several tries to intern and use a symbol in clisp. [1]> (setq sym (intern "foo")) |foo| [2]> (eq sym 'foo) NIL Why??? [3]> (defun internup (me &optional (package *package*)) (intern (string-upcase me) package)) INTERNUP [4]> (eq 'abc (internup "abc")) T Probably must be upcase. [12]>(let ((abc 2)) (eval '(+ 2 abc))) *** - EVAL: variable ABC has no value The following restarts are available: ok [18]> (let ((abc 2)) (eval '(+ 2 'abc))) *** - +: ABC is not a number The following restarts are available: Interesting. Should I set it before. [14]> (setq a (internup "abc"))