lisp

Meaning of 'quote in Lisp

吃可爱长大的小学妹 提交于 2019-12-11 06:26:20
问题 This question arose when reading SICP . Why (list 'quote '(a b c)) evaluated by the interpreter ( R5RS in Dr.Racket ) as '(a b c) . For me it should be (quote (a b c)) . For instance (list 'quot '(a b c)) is evaluated as (quot (a b c)) . What is so special in the 'quote ? 回答1: It also took me a while to understand this problem. But it's just your good-hearted lisp interpreter showing (quote (a b c)) in it's equivalent form '(a b c). Since there is no such equivalence/syntactic sugar for

Difference between '(()) and (cons null null)

醉酒当歌 提交于 2019-12-11 05:59:22
问题 I am confused about the difference between '(()) and (cons null null) in scheme. The code below show that b and c are completely the same thing. (define (dup2 x) (let ((d '(()))) (set-car! d (car x)) (set-cdr! d (cdr x)) d)) (define a '(1 2)) (define b (dup2 a)) (define c (dup2 a)) (set-car! b 2) > c ;; --> (2 2) However, when I used dup instead of dup2 : (define (dup x) (let ((d (cons null null))) (set-car! d (car x)) (set-cdr! d (cdr x)) d)) (define a '(1 2)) (define b (dup a)) (define c

Listing directories in CLISP

馋奶兔 提交于 2019-12-11 05:08:02
问题 I've been trying to see get a list of all files within a directory in CLISP, but I've only been able to get all non-directory files within a directory. I'm currently trying this in Windows 7 with cygwin, so that may influence my results. I'm pretty new to CLISP (and LISP over all), and what I'm currently trying to do is as follows: (directory (make-pathname :directory '(:absolute "cygdrive" "c" "Download") :name :wild)) This successfully returns all non-directory files within "C:\Download.

The Order of Variable and Function Definitions

倾然丶 夕夏残阳落幕 提交于 2019-12-11 05:04:37
问题 Why is it that: Function definitions can use definitions defined after it while variable definitions can't. For example, a) the following code snippet is wrong: ; Must define function `f` before variable `a`. #lang racket (define a (f)) (define (f) 10) b) While the following snippet is right: ; Function `g` could be defined after function `f`. #lang racket (define (f) (g)) ; `g` is not defined yet (define (g) 10) c) Right too : ; Variable `a` could be defined after function `f` #lang racket

Typesafe and argument safe division in common lisp

大憨熊 提交于 2019-12-11 04:51:01
问题 Long story short I need to defun ts_div and allow it to be a typesafe and "argument safe" version of the regular / Basically, I want it to accept a list with any number of numbers in it (even none), and be able to call it like this: (ts_div (123 321 23)) or: (ts_div somelist) Desired result: if there are more than two items in the list, the first will be divided by the second, and the rest is ignored. If the second number is 0, it should return the value of the first number instead. If the

define-syntax issue in scheme

孤人 提交于 2019-12-11 04:29:48
问题 I'm wondering what is going wrong with how I'm defining my for-loop in scheme. Whenever I try running a for statement with it it runs for quite a while and then crashes. (define-syntax for (syntax-rules (:) [(_ (initial : test : update) body) (begin initial (if test (begin body update (for [test : update] body))))] [(_ (test : update) body) (if test (begin body update (for [test : update] body)))])) It should run the initial condition, check the test, run the body, and then loop to the next

Implementing has-list in scheme

对着背影说爱祢 提交于 2019-12-11 04:14:27
问题 how can you implement a Scheme function has-list recursively, which tests whether a list contains other list as an element. For example (has-list '(1 2 3)) should return false, and (has-list '(1 2 (3 4) 5)) should return true. 回答1: If your implementation has something like ormap , then: (define (has-list? l) (ormap list? l)) Using or as in Dan D.'s answer will not work. 回答2: A list has a list as an element iff it is not the empty list and either its first element is a list or the rest of the

Identify C++ style comments with asm-mode in Emacs Lisp

余生长醉 提交于 2019-12-11 04:03:44
问题 In Emacs, how do I tweak the lisp file(s) to identify C++ style comments in asm-mode? I have a compiler that deals with asm code but uses C++ style comments. I would like emacs identify C++ style commenting and show chunks of text wrapped in /* , */ as comments. The default comment start characters of asm-mode (?\;) can be ignored. 回答1: Are you using Emacs 21? If I'm reading the version history correctly, // style comments are supported starting with GNU Emacs 22. M-x emacs-version tells you

Giving a symbol a negative value in lisp

为君一笑 提交于 2019-12-11 03:43:09
问题 I'm very new to lisp and I am working on basic syntax. I am trying to convert: r1 = (-b + sqrt(b^2 - 4*a*c))/(2*a) into a lisp format. The only problem I think I am having is that I cannot get lisp to recognize -b as the negative value of my symbol b. This is what I have so far from the lisp prompt: [17]> (setq a 1L0) 1.0L0 [18]> (setq b -1L0) -1.0L0 [19]> (setq c -1L0) -1.0L0 [20]> (setq r1 (+ (/ (sqrt (- (power b 2) (* (* 4 a) c))) (* 2 a)) -b)) *** - EVAL: variable -B has no value The

How to traverse a tree in Clojure while collecting the value from each node node?

好久不见. 提交于 2019-12-11 03:29:43
问题 I want to create a function that collects the value from each node in a binary tree. In the ClojureDocs, I found several functions for traversing a tree/graph, such as tree-seq, prewalk, and postwalk. https://clojuredocs.org/clojure.core/tree-seq https://clojuredocs.org/clojure.walk/prewalk https://clojuredocs.org/clojure.walk/postwalk Can any of these be used to accumulate the value of nodes traversed? As a Clojure newby, I don't see how to do it. If you know how (in Clojure or similar Lispy