lisp

Problem with list in Lisp

本小妞迷上赌 提交于 2020-01-04 06:04:43
问题 I am trying to write a simple procedure in Lisp to insert an element into binary search tree. I represented the tree as a list: the first element in the tree is the root the second element is the left sub-tree the third element is the right sub-tree This is my code: (define Insert (lambda (x T) (if (null? T) (list x '() '()) (if (> x (car T)) (list (car T) (cadr T) (Insert x (list (caddr T)))) (list (car T) (Insert x (cadr T)) (list (caddr T))))))) When I call the procedure like this: (Insert

Efficient incremental hash computation during program interpretation

那年仲夏 提交于 2020-01-04 04:05:08
问题 I'd like to write a recursively memoizing Scheme interpreter . At any point during evaluation, the interpreter should be able to detect when it receives as arguments a pair of expression and environment that it has previously seen. Plain memoization of eval and apply is inefficient . It would require looking up the arguments in a hash table on every call of eval / apply , which would require walking the entire (possibly big) arguments on hash table matches. For example, assume that the

Decimal to Binary in Lisp - make a non-nested list

拥有回忆 提交于 2020-01-04 02:54:12
问题 When reaching my recursion cases, I use list to append the future result with the current one, but I end up with a nested list because of recursion. This causes an error when I have a number that causes recursion for more than five times. Any ideas how I can get results in a single plain non-nested list, e.g.: CL-USER 100 : 8 > (BINARY_LIST 4) (1 0 0) Code & Example output: CL-USER 99 : 8 > (defun binary_list (i) (COND ((= i 0) 0) ((= i 1) 1) ((= (mod i 2) 0) (list (binary_list (truncate i 2)

Using scheme/racket to return specific items from a list

[亡魂溺海] 提交于 2020-01-04 02:30:48
问题 What I would like to do is create a function that takes in a list of values and a list of characters and coalesce the corresponding characters("atoms" I think they would technically be called) into a new list. Here is what I have so far; #lang racket (define (find num char) (if (= num 1) (car char) ;Problem here perhaps? (find (- num 1) (cdr char)))) (define (test num char) (if (null? num) '("Done") (list (find (car num) (test (cdr num) char))))) This however gives me an error, which for the

exp in SBCL is wrong?

倖福魔咒の 提交于 2020-01-03 18:35:12
问题 CL-USER> (exp 1) 2.7182817 Why? It should be 2.7182818 (rounded from 2.7182818284590452353602874713526624977572470936999595749669...) SBCL 1.0.29.11.debian 回答1: It is a rounding error inherent in the IEEE single-float format. CL-USER> (exp 1.0d0) 2.718281828459045d0 回答2: Learn more about floating point arithmetic and its 'problems' in this classic paper (PDF): What Every Computer Scientist Should Know About Floating-Point Arithmetic 来源: https://stackoverflow.com/questions/4771346/exp-in-sbcl

Boolean values: t vs. nil vs 1 vs -1

最后都变了- 提交于 2020-01-03 17:47:49
问题 In Elisp, I've encountered different APIs for modeling boolean values. I was under the impression that t and nil were the idiomatic ways of representing true and false respectively. However, I've also seen 1 and -1 used to model the same thing. What confuses me is that I have come across APIs that won't work if nil is supplied but will work if -1 is used. Can someone help me understand which is in fact the preferred way. And if the answer is t and nil , I welcome any theories on why some

What exactly is the SEXP data type in R's C API and why is it used? [closed]

你。 提交于 2020-01-03 15:21:15
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . I am aware of the wikipedia page on SEXP, and I know that it stands for symbolic expression. I know (vaguely) SEXP is notation to refer to tree data structures in Lisp, but I want to know what motivated the developers to call the data type of R objects in C SEXP. Why SEXP? I am

Inferior Shell or UIOP: Interacting with background process

这一生的挚爱 提交于 2020-01-03 13:36:59
问题 So, I got past actually getting a program to run from SBCL Lisp using inferior-shell (presumably UIOP would be just fine). Anyway, now that I can do that, I still have no clue how to interact with the program if it is running in the background. All of this functionality like pipes and streams connected to the stdin and stdout of the running program are advertised, just not documented. It would seem like this is the most basic thing to do. Does anybody have an example of doing such a thing?

Floating Point Precision Error

耗尽温柔 提交于 2020-01-03 13:15:08
问题 I am having problem with the LISP expression below. There is floating precision error while doing sum for floating point numbers. CL-USER> (+ -380 -158.27 -35.52) Actual: -573.79004 Expected: -573.79000 Please suggest me how can I achieve the expected result in LISP (I am using Lispworks). 回答1: Floats are not exact Floats represent a subset of mathematical reals with certain precision , they are not exact numbers. Round off errors are inevitable . The ANSI Common Lisp standard provides for 4(

Scheme: change value of an element in a list

孤者浪人 提交于 2020-01-03 09:37:11
问题 I hate using SO as a way to find simple functions, but I really can't find a function like this anywhere: Given a list (1 2 3 4 5), I'd like the equivalent of (PHP's, Perl's, Python's) $a = array(1, 2, 3, 4, 5); $a[3] = 100; Which results in (1 2 3 100 5) Thanks! 回答1: You can write list-set! of Guile, like so: (define a (list 1 2 3 4)) ; a is '(1 2 3 4) (define (list-set! list k val) (if (zero? k) (set-car! list val) (list-set! (cdr list) (- k 1) val))) (list-set! a 2 100) ; a is '(1 2 100 4)