lisp

What is the difference between 1 and '1 in Lisp?

戏子无情 提交于 2019-12-20 09:13:41
问题 I had never really thought about whether a symbol could be a number in Lisp, so I played around with it today: > '1 1 > (+ '1 '1) 2 > (+ '1 1) 2 > (define a '1) > (+ a 1) 2 The above code is scheme, but it seems to be roughly the same in Common Lisp and Clojure as well. Is there any difference between 1 and quoted 1? 回答1: Well, they are in fact very different. '1 is however precisely the same as (quote 1) . (car ''x) evaluates to the symbol 'quote'. 1 is an S-expression, it's the external

In what sense are languages like Elixir and Julia homoiconic?

折月煮酒 提交于 2019-12-20 08:47:21
问题 Homoiconicity in Lisp is easy to see: (+ 1 2) is both the function call to + with 1 , 2 as arguments, as well as being a list containing + , 1 , and 2 . It is simultaneously both code and data. In a language like Julia, though: 1 + 2 I know we can parse this into an Expr in Julia: :(1 + 2) And then we can get the AST and manipulate it: julia> Meta.show_sexpr(:(1+2)) (:call, :+, 1, 2) So, we can manipulate a program's AST in Julia (and Elixir). But are they homoiconic in the same sense as Lisp

How does one implement a “stackless” interpreted language?

梦想的初衷 提交于 2019-12-20 08:39:23
问题 I am making my own Lisp-like interpreted language, and I want to do tail call optimization. I want to free my interpreter from the C stack so I can manage my own jumps from function to function and my own stack magic to achieve TCO. (I really don't mean stackless per se, just the fact that calls don't add frames to the C stack. I would like to use a stack of my own that does not grow with tail calls). Like Stackless Python, and unlike Ruby or... standard Python I guess. But, as my language is

Practical example of Lisp's flexibility? [closed]

匆匆过客 提交于 2019-12-20 08:17:24
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . Someone is trying to sell Lisp to me, as a super powerful language that can do everything ever, and then some. Is there a practical

Using reduce over a tree in Lisp

百般思念 提交于 2019-12-20 04:50:13
问题 To fold a flat list in Lisp you use reduce : * (reduce #'+ '(1 2 3 4 5)) 15 But what if I have an arbitrarily complex tree, and I want to apply a function over each of the element? So that fold over '(1 (2) (3 (4) 5)) would still give 15 ? I tried to use reduce , but I had to provide a custom function, which kinda defeats the purpose: (defun tree+ (a b) (cond ((null b) 0) ((atom b) (+ a b)) (t (+ (tree+ a (car b)) (tree+ 0 (cdr b)))))) (reduce #'tree+ '(1 (2) (3 (4) 5)) :initial-value 0) ;

How to order my accumulate variable in this case on Racket?

陌路散爱 提交于 2019-12-20 04:36:31
问题 I am coding with Racket for educational reasons. I was given a task in which I should create a function that, without filter, would receive a list as an input and return another list only with the even numbers of the first list. I came up with this recursive definition of an iterative process: (define (add-even lista) (define (iter lista accu) (cond ((null? lista) accu) ((even? (car lista)) (iter (cdr lista) (cons (car lista) accu))) (else (iter (cdr lista) accu)))) (iter lista empty)) It

Go back to last state

倖福魔咒の 提交于 2019-12-20 04:09:28
问题 Is there a way to go back to last state of the runtime? In short, save the runtime and reload it. But the core image is too big (I'm making a small game :-), so I come up with an idea, save some important data, and start running at certain line (maybe also the stack info). For example: (defun save () _do-some-magic-to-save-the-state-and-then-exit_) (defvar data (list 'a 'b 'c)) ; important data (format t "Hello ") (save) (format t "World!~%") Next time, the software can start at the point

Lisp Append Not Working Properly

别说谁变了你拦得住时间么 提交于 2019-12-20 03:17:56
问题 Hi I am trying append a simple element to a lisp list. (append queue1 (pop stack1)) I thought the above code would append the first element of stack1 to queue1. Does queue1 need to be non nil? Thanks. 回答1: Append returns the concatenated list ( queue1 with the first element of stack1 appended). It does not modify queue1. The destructive equivalent of append is nconc: this appends to the list "in place." 回答2: You did not specify which Lisp do you mean, but in Common Lisp at least: APPEND

Longest decreasing sequence in Lisp

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-20 02:56:22
问题 I'm working on some problems for my upcoming exam and I need some help with this Lisp function. I'm working in CLISP. I have to find the longest decreasing sequence comprised only of odd numbers in a list. Example: (longest '(13 9 3 7 4 7 5 3 2 8 15 11 9 7 3)) Should return: (15 11 9 7 3) The only mandatory requirement is that that the function has to be implemented recursively :) 回答1: With contiguous subsequences, it's easy. Except I don't lisp, so I have to explain it in words. Call a

Is defun or setf preferred for creating function definitions in common lisp and why?

人盡茶涼 提交于 2019-12-20 02:32:49
问题 What is the fundamental difference in the functions defined using defun and setf as below and is one method preferred over another outside of style considerations? Using defun : * (defun myfirst (l) (car l) ) MYFIRST * (myfirst '(A B C)) A Using setf : * (setf (fdefinition 'myfirst) #'(lambda (l) (car l))) #<FUNCTION (LAMBDA (L)) {10021B477B}> * (myfirst '(A B C)) A If, as according to Wikipedia: named functions are created by storing a lambda expression in a symbol using the defun macro