lisp

Lisp illegal function call,

你说的曾经没有我的故事 提交于 2019-12-12 22:38:03
问题 The code below keeps throwing the following error: caught ERROR: illegal function call (LET ((SOLUTION 'NIL) (FIRST 0) (SECOND 0)) (DOLIST (EL LST) (IF (NUMBERP EL) (PUSH EL SOLUTION) ((SETF #) (SETF #) (PUSH # SOLUTION)))) (CAR SOLUTION)) Can anyone see why? Syntactically I can't see anything wrong with it. Note: I'm using sbcl. My code: (defun evalpostfix (lst) (let ((solution '()) (first 0) (second 0)) (dolist (el lst) (if (numberp el) ;if (push el solution) ;then ((setf second (pop

How do you detect key presses on a Racket web application?

荒凉一梦 提交于 2019-12-12 21:02:28
问题 I've been through the documentation for web-servers and can't find anything on it. Here's my code for a basic web application: #lang racket (require web-server/servlet web-server/servlet-env) (define test '()) (define (start request) (define bindings (request-bindings request)) (cond ((exists-binding? `cb1 bindings) (set! test '(1 2 3)) (printf "~a" "(test) has been set to '(1 2 3)!"))) (response/xexpr `(html (head (title "My Blog")) (body (h1 "Under construction") (form ,`(input ((name "cb1"

How to implement closures into a LISP interpreter

守給你的承諾、 提交于 2019-12-12 19:55:09
问题 I'm currently working on a LISP interpreter written in Java. Now I stuck at the closures. I want to enable closures like this: (define a 1000) (define closure (lambda (a) (lambda (b) (+ a b)))) (define x (closure 10)) (x 20) --> 30 So, (x 20) should return 30 . But, guess what, it returns 1020 in my interpreter. I think the mistake is in my lambda class. It looks like this: public class LLambda extends LOperation { private LList parameters; private LList definitions; public LLambda(LList

How do I access an unknown instance's slot using a string?

邮差的信 提交于 2019-12-12 19:32:03
问题 Problem Given an instance, inst and a string attr containing the name of a slot, how can I obtain the value of the slot attr on inst ? Of course, if attr were a symbol rather than a string, I would typically just use (slot-value inst attr) , but it seems I need the package information to properly call intern (see below). Minimal example (defpackage :pack1 (:use :common-lisp) (:export :*inst*)) (in-package :pack1) (defclass temp-class () ((temp-slot :initarg :temp-slot))) (defvar *inst* (make

External vs Internal Symbols in Common Lisp Package

我是研究僧i 提交于 2019-12-12 19:20:57
问题 What is the difference between them in the context of a Common Lisp package? I am reading through SLIME documentation and some commands mention that extensively. 回答1: The author of a Common Lisp package can export a symbol for the user of the package. Then the symbol is an external symbol and you can access it with package-name:external-symbol-name . Internal symbols are not meant for the user but can be accessed with package-name::symbol-name More explanations are in Peter Seibel's book and

Mysterious problem with floating point in LISP - time axis generation

☆樱花仙子☆ 提交于 2019-12-12 19:12:47
问题 Ok, I do know what and how works floating point. But, It doesn't stop to puzzle me whenever I deal with it. I try to do some time axis generation function. The idea is simple. To make smt like this (defun make-time-axis (start step stop) ...) So, when you call it with e.g. (make-time-axis 0 0.1 1.2) result will be 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1 But whenever I do whatever (loop for i from 0.0 below 1.2 by 0.1 collect i) or (defun make-time-axis (last step stop) (cond ((< last stop

Translating this to Common Lisp

好久不见. 提交于 2019-12-12 17:33:28
问题 I've been reading an article by Olin Shivers titled Stylish Lisp programming techniques and found the second example there (labeled "Technique n-1") a bit puzzling. It describes a self-modifying macro that looks like this: (defun gen-counter macro (x) (let ((ans (cadr x))) (rplaca (cdr x) (+ 1 ans)) ans)) It's supposed to get its calling form as argument x (i.e. (gen-counter <some-number>) ). The purpose of this is to be able to do something like this: > ;this prints out the numbers from 0 to

unfold function in scheme

守給你的承諾、 提交于 2019-12-12 16:17:36
问题 Goal: implement unfold function using only two arguments. The arguments: the first argument is f which takes an initial value of some type I and returns nil or a cons pair of two elements (the first of these two is the next element that goes in the list of some type A and the next initial value again of some type I). The second argument is an initial value of some type I and the return is a list of items of type A. This is what I have so far and I am not sure why it is not working: (define

Idiomatic way to group a sorted list of integers?

那年仲夏 提交于 2019-12-12 15:48:49
问题 I have a sorted list of integers, (1 2 4 5 6 6 7 8 10 10 10) . I want to group them all, so that I get ((1) (2) (4) (5) (6 6) (7) (8) (10 10 10)) . So far I have this, which works: (let ((current-group (list)) (groups (list))) (dolist (n *sorted*) (when (and (not (null current-group)) (not (eql (first current-group) n))) (push current-group groups) (setf current-group (list))) (push n current-group)) (push current-group groups) (nreverse groups)) But I'm sure there must be a much more LISPy

Print adjacent duplicates of a list (scheme)

不问归期 提交于 2019-12-12 15:31:00
问题 I'm trying to create a function that returns the adjacent duplicates of a list, for example (dups '(1 2 1 1 1 4 4) should return the list (1 4). This is the code I came up with so far: (define (dups lst) (if (equal? (car lst)(car(cdr lst))) (cons(cdr lst) '()) (dups(cdr lst)))) This function doesn't return all the adjacent duplicates, it only returns the first adjacent duplicates! How can I fix it so that it returns all the adjacent duplicates of a list? Thank you. 回答1: Once your code finds a