lisp

(define (average …)) in Lisp

蓝咒 提交于 2019-12-04 05:50:40
I'm just playing around with scheme/lisp and was thinking about how I would right my own definition of average . I'm not sure how to do some things that I think are required though. define a procedure that takes an arbitrary number of arguments count those arguments pass the argument list to (+) to sum them together Does someone have an example of defining average ? I don't seem to know enough about LISP to form a web search that gets back the results I'm looking for. The definition would be a very simple one-liner, but without spoiling it, you should look into: a "rest" argument -- this

I'm trying to figure out how to incorporate 3 variables into my tail recursion code for racket

旧巷老猫 提交于 2019-12-04 05:19:39
问题 Write a tail recursive function called popadd that models a population with P people at time t = 0 and adds d people per year. (define (popadd t P) (if (= t 0) P (+(popadd( - t 1) P)d)) ) but, of course, I get the error that d hasn't been defined yet, which is true. I tried adding it as an input, but as a return I get the number inserted for D. 回答1: You can simply pass along another parameter to the recursion: (define (popadd t P d) (if (= t 0) P (+ d (popadd (- t 1) P d)))) Or you can define

How to replace a running function in Common Lisp?

时光毁灭记忆、已成空白 提交于 2019-12-04 05:18:12
Suppose we use SBCL's #'save-lisp-and-die to create an server applicatioon App1, which works very well. Now we want to replace a function #'func1 with a new version without stopping App1. How can we do it in Common Lisp ? Any suggestion is appreciated ! You need to load the new function definition. Then new function will be available immediately; code will call newly loaded function. New function definition may be loaded in many ways: (load (compile-file "file.lisp")) where file.lisp is a source code for function (load "file.fasl") where file.fasl is compiled source code (eval (defun ...)) Of

Uninterned symbols in Common Lisp

对着背影说爱祢 提交于 2019-12-04 04:52:32
Several times I came across the notion of uninterned symbols, but I am not entirely clear about what they are. Is there a way to intern a symbol created with (make-symbol)? Can I assign a value to a symbol without interning it? Is it possible to rename a symbol (interned or uninterned)? What else can one do with an uninterned symbol? Update: What is happening with symbols in this piece of code? CL-USER> (defun func () (let ((var 'sym)) (print (find-symbol "sym")) (print var))) FUNC CL-USER> (func) NIL SYM SYM My incorrect understanding is: 1. find-symbol prints nil, so the symbol is not

What is the Scheme function to find an element in a list?

回眸只為那壹抹淺笑 提交于 2019-12-04 04:46:19
I have a list of elements '(a b c) and I want to find if (true or false) x is in it, where x can be 'a or 'd, for instance. Is there a built in function for this? If you need to compare using one of the build in equivalence operators, you can use memq , memv , or member , depending on whether you want to look for equality using eq? , eqv? , or equal? , respectively. > (memq 'a '(a b c)) '(a b c) > (memq 'b '(a b c)) '(b c) > (memq 'x '(a b c)) #f As you can see, these functions return the sublist starting at the first matching element if they find an element. This is because if you are

Get index of list within list in Lisp

余生颓废 提交于 2019-12-04 04:46:06
If I have a list like this ((0 1 2) (3 4 5) (6 7 8) (0 3 6) (1 3 7) (2 4 8) (0 4 8) (2 4 6)) And I want to find the index of (0 3 6) , is there a built-in function to do this? POSITION doesn't seem to work when the search item is itself a list. See hyperspec . POSITION can take a :test argument: (position '(0 3 6) '((0 1 2) (3 4 5) (6 7 8) (0 3 6) (1 3 7) (2 4 8) (0 4 8) (2 4 6)) :test #'equal)) 3 The default test for POSITION (and other sequence operations) is EQL, by the way. 来源: https://stackoverflow.com/questions/4288292/get-index-of-list-within-list-in-lisp

Resolving symbols in a Common Lisp list

大兔子大兔子 提交于 2019-12-04 04:23:50
问题 Suppose I have a function CL-USER> (defun trimmer (seq) "This trims seq and returns a list" (cdr (butlast seq))) TRIMMER CL-USER> (trimmer '(1 2 3 VAR1 VAR2)) (2 3 VAR1) CL-USER> Notice how, due to QUOTE, VAR1 and VAR2 are not resolved. Suppose I want to resolve the symbols VAR1 and VAR2 to their values - is there a standard function to do this? 回答1: Do not use quote to create a list with variables; use list instead: CL-USER> (trimmer (list 1 2 3 var1 var2)) (2 3 value-of-var1) (where value

Modification of the basic if expression in Scheme. Why does it go into an infinite loop?

冷暖自知 提交于 2019-12-04 04:22:44
问题 In Scheme, I modified the basic 'if' command as: (define (modified-if predicate then-clause else-clause) (if predicate then-clause else-clause)) And then I defined a simple factorial generating program using the modified version of if: (define (factorial n) (modified-if (= n 0) (* n (factorial (- n 1))))) Now, when I call the above function, it goes into an infinite loop. Why does that happen? 回答1: Scheme has eager evaluation. This means that, unless you're using a special form (like if ) or

how to answer yes or no automatically in emacs

雨燕双飞 提交于 2019-12-04 03:52:37
I binded function semantic-symref to key C-c C-r like this: (global-set-key (kbd "C-c C-r") 'semantic-symref) everytime I pressed C-c C-r , it prompted: Find references for xxxxx? (y or n) How can I answer it automatically? I tryed using lambda function like this, but failed (global-set-key (kbd "C-c C-r") (lambda() (interactive) (semantic-symref "yes"))) The answer by @huitseeker is quite neat and effective. After four years, with flet and defadvice being obsolete, I wrote the following functions to answer yes automatically. Maybe it's useful for someone. (defun my/return-t (orig-fun &rest

How can I reuse a gethash lookup in Common Lisp?

拈花ヽ惹草 提交于 2019-12-04 03:10:51
I have a hash table where the keys are rather complex lists, with sublists of symbols and integers, and the value should be modified depending on the already existing value. The table is created with :test #'equal . I do something similar to this a lot: (defun try-add (i) (let ((old-i (gethash complex-list table nil))) (if (may-add old-i) (push i (gethash complex-list table))))) Profiling shows that equal tests take a lot of time. I have an optimization idea, that the amount of gethash lookups could be reduced from two to one. It can be done in C++ by reusing the iterator, but not sure how