lisp

LISP: Keyword parameters, supplied-p

送分小仙女□ 提交于 2019-12-05 17:07:36
问题 At the moment I'm working through "Practical Common Lisp" from Peter Seibel. In the chapter "Practical: A Simple Database" (http://www.gigamonkeys.com/book/practical-a-simple-database.html) Seibel explains keyword parameters and the usage of a supplied-parameter with the following example: (defun foo (&key a (b 20) (c 30 c-p)) (list a b c c-p)) Results: (foo :a 1 :b 2 :c 3) ==> (1 2 3 T) (foo :c 3 :b 2 :a 1) ==> (1 2 3 T) (foo :a 1 :c 3) ==> (1 20 3 T) (foo) ==> (NIL 20 30 NIL) So if I use

What is happening with this Common Lisp code?

女生的网名这么多〃 提交于 2019-12-05 16:53:42
问题 I've written the following bit of code to simulate rolling a six-sided die a number of times and counting how many times each side landed up: (defun dice (num) (let ((myList '(0 0 0 0 0 0))) (progn (format t "~a" myList) (loop for i from 1 to num do (let ((myRand (random 6))) (setf (nth myRand myList) (+ 1 (nth myRand myList))))) (format t "~a" myList)))) The function works great the first time I call it, but on subsequent calls the variable myList starts out at the value it had at the end of

find free variables in lambda expression

江枫思渺然 提交于 2019-12-05 16:25:12
问题 Does anyone know how I can figure out the free variables in a lambda expression? Free variables are the variables that aren't part of the lambda parameters. My current method (which is getting me nowhere) is to simply use car and cdr to go through the expression. My main problem is figuring out if a value is a variable or if it's one of the scheme primitives. Is there a way to test if something evaluates to one of scheme's built-in functions? For example: (is-scheme-primitive? 'and) ;Value:

lisp sort list via function

大城市里の小女人 提交于 2019-12-05 15:49:31
I am trying to use lisp's sort to sort a list via a function but dont have a clue how to do this. I have a start-point in 2D Space with x and y coordinates. Then i have a List of N-other points and i have a function that calculates the distance between 2 points. What I want now is a list, that contains all the N-Points and is sorted by distance ascending from the start-point to all other points. I think I can use the sort-function and pass a function as argument (the calculate-distance function) But i dont know how to do it and researches on the web did not help. Any ideas? Regards Use :key

What are the benefits of letrec?

孤人 提交于 2019-12-05 13:31:21
问题 While reading "The Seasoned Schemer" I've begun to learn about letrec . I understand what it does (can be duplicated with a Y-Combinator) but the book is using it in lieu of recurring on the already define d function operating on arguments that remain static. An example of an old function using the define d function recurring on itself (nothing special): (define (substitute new old l) (cond ((null? l) '()) ((eq? (car l) old) (cons new (substitute new old (cdr l)))) (else (cons (car l)

Why does function apply complain about long lists?

柔情痞子 提交于 2019-12-05 13:24:53
As part of some Eulerian travails , I'm trying to code a Sieve of Eratosthenes with a factorization wheel. My code so far is: (defun ring (&rest content) "Returns a circular list containing the elements in content. The returned list starts with the first element of content." (setf (cdr (last content)) content)) (defun factorization-wheel (lst) "Returns a circular list containing a factorization wheel using the list of prime numbers in lst" (let ((circumference (apply #'* lst))) (loop for i from 1 to circumference unless (some #'(lambda (x) (zerop (mod i x))) lst) collect i into wheel finally

Common LISP on iPhone/iOS

风格不统一 提交于 2019-12-05 13:22:01
问题 Is it possible to call a Common Lisp function in iOS? If so, is it possible create it in a dynamic library? 回答1: It depends on what you mean by calling a CL function, but most likely ECL will be your shortest path. Start here, as there is a patch for ECL to better accommodate iOS: http://funcall.posterous.com/tag/iphone ECL generates C code, so you should be in safe territory with Apple's shifting policies. 回答2: Have you tried MOCL? According to the website "mocl is a highly optimizing CL

fixed point combinator in lisp

无人久伴 提交于 2019-12-05 13:08:54
;; compute the max of a list of integers (define Y (lambda (w) ((lambda (f) (f f)) (lambda (f) (w (lambda (x) ((f f) x))))))) ((Y (lambda (max) (lambda (l) (cond ((null? l) -1) ((> (car l) (max (cdr l))) (car l)) (else (max (cdr l))))))) '(1 2 3 4 5)) I wish to understand this construction. Can somebody give a clear and simple explanation for this code? For example, supposing that I forget the formula of Y. How can I remember it , and reproduce it long after I work with it ? Will Ness Here's some related answers (by me): Y combinator discussion in "The Little Schemer" Unable to get

why defun is not the same as (setq <name> <lambda>)?

杀马特。学长 韩版系。学妹 提交于 2019-12-05 12:44:20
问题 I'm confused about how defun macro works, because (defun x () "hello") will create function x, but symbol x still will be unbound. If I'll bind some lambda to x then x will have a value, but it will not be treated by interpreter as function in form like this: (x) I think that it is related to the fact that defun should define function in global environment, but I'm not sure what does it exactly mean. Why can't I shadow it in the current environment? Is there any way to force interpreter treat

How do I ask the Lisp compiler to ignore a (label-variety) function?

半腔热情 提交于 2019-12-05 12:25:18
I've stared at Steele's Common Lisp the Language until I'm blue in the face, and still have this question. If I compile: (defun x () (labels ((y ())) 5)) (princ (x)) (terpri) this happens: home:~/clisp/experiments$ clisp -c -q x.lisp ;; Compiling file /u/home/clisp/experiments/x.lisp ... WARNING in lines 1..3 : function X-Y is not used. Misspelled or missing IGNORE declaration? ;; Wrote file /u/home/clisp/experiments/x.fas 0 errors, 1 warning home:~/clisp/experiments$ Fair enough. So how do I ask the compiler to ignore function y? I tried this: (defun x () (labels (#+ignore(y ())) 5)) (princ