lisp

In common-lisp, how do I modify part of a list parameter from within a function without changing the original list?

我怕爱的太早我们不能终老 提交于 2019-11-28 01:01:39
问题 I'm trying to pass a list to a function in Lisp, and change the contents of that list within the function without affecting the original list. I've read that Lisp is pass-by-value, and it's true, but there is something else going on that I don't quite understand. For example, this code works as expected: (defun test () (setf original '(a b c)) (modify original) (print original)) (defun modify (n) (setf n '(x y z)) n) If you call (test), it prints (a b c) even though (modify) returns (x y z).

Why multiple namespaces?

只愿长相守 提交于 2019-11-27 23:37:37
What is the rationale behind the design decision to have separate namespaces for values and functions in Common Lisp? What are the arguments for and against it? Common Lisp is basically a descendant from the original Lisp 1.5, or rather, a unification of its diverging dialects. The original Lisp 1.5 was what is nowadays called a Lisp-2. Because it was back in the sixties and the fact that you could pass functions to other functions was weird enough. No one would even think of letting them share the same namespace. Almost any language invented today with support for higher order functions and

Strange Lisp Quoting scenario - Graham's On Lisp, page 37

早过忘川 提交于 2019-11-27 23:32:54
问题 I'm working my way through Graham's book "On Lisp" and can't understand the following example at page 37: If we define exclaim so that its return value incorporates a quoted list, (defun exclaim (expression) (append expression ’(oh my))) > (exclaim ’(lions and tigers and bears)) (LIONS AND TIGERS AND BEARS OH MY) > (nconc * ’(goodness)) (LIONS AND TIGERS AND BEARS OH MY GOODNESS) could alter the list within the function: > (exclaim ’(fixnums and bignums and floats)) (FIXNUMS AND BIGNUMS AND

How can I simulate macros in JavaScript?

旧城冷巷雨未停 提交于 2019-11-27 20:09:59
I know that JavaScript doesn't support macros (Lisp-style ones) but I was wondering if anyone had a solution to maybe simulate macros? I Googled it, and one of the solutions suggested using eval(), but as he said, would be quite costly. They don't really have to be very fancy. I just want to do simple stuff with them. And it shouldn't make debugging significantly harder :) You could use parenscript . That'll give you macros for Javascript. A library by Mozilla (called SweetJS ) is designed to simulate macros in JavaScript. For example, you can use SweetJS to replace the function keyword with

Separate Namespaces for Functions and Variables in Common Lisp versus Scheme

冷暖自知 提交于 2019-11-27 20:03:49
Scheme uses a single namespace for all variables, regardless of whether they are bound to functions or other types of values. Common Lisp separates the two, such that the identifier "hello" may refer to a function in one context, and a string in another. (Note 1: This question needs an example of the above; feel free to edit it and add one, or e-mail the original author with it and I will do so.) However, in some contexts, such as passing functions as parameters to other functions, the programmer must explicitly distinguish that he's specifying a function variable, rather than a non-function

Can you program without REPL on Lisp?

我的未来我决定 提交于 2019-11-27 19:35:03
问题 So I just got Land of Lisp and started to do the first program. I have a couple questions. Is there a way to just write some code and run it through a compiler, or interpreter, and not use the REPL thing? I don't like it much. I can't seem to go back if I messed up. It just kinda says "Ha you screwed up, retype that whole function." I would also like to know what the point of REPL is. 回答1: Non-REPL work flow Edit your file Compile the file using compile-file; fix errors and warnings; repeat.

Swapping elements in a Common Lisp list

北慕城南 提交于 2019-11-27 17:44:42
问题 Is there a Common Lisp function that will swap two elements in a list given their indices and return the modified list? 回答1: You can use rotatef: (rotatef (nth i lst) (nth j lst)) Of course, list indexing can be expensive (costing O( size of list )), so if you do this with any regularity, you'd rather want to use an array: (rotatef (aref arr i) (aref arr j)) 回答2: I would avoid indexing into the list twice by using nthcdr to get the cdr of the cons cell containing the first element that you

What is the best Scheme implementation for working through SICP?

邮差的信 提交于 2019-11-27 17:14:34
I have been using PLT Scheme , but it has some issues. Does anyone know of a better implementation for working through SICP? Use MIT Scheme . It's recommended by the authors of SICP, and is used at MIT for the 6.001: Structure and Interpretation of Computer Programs course. Use Racket (formerly PLT Scheme). The DrRacket IDE is an excellent starting point for all things Scheme including SICP. To look up keywords in the documentation, place the cursor on the keyword and press F1 . In DrRacket you can now see the images directly in the REPL (the read-eval-print-loop). SICP Support for DrRacket ,

clojure 新手指南(2)使用REPL求值

蓝咒 提交于 2019-11-27 16:50:47
Clojure 拥有动态语言的所有好处。这意味着你可以在程序被加载后依然可以改变它,并且不用采取额外的步骤去编译代码。你既不用停止也不用重启正在运行的应用就可以让修改生效。这对于其他语言来说可是一个非常显著的优势,特别是如果你正打算将变化动态地(不用停止和重启服务器)呈献给用户时。 要想改变一个已经加载的程序,你唯一需要的是使用REPL去加载变化的地方。REPL是一个独立的程序,Clojure利用它提供给你和它的编译器直接交互的功能,当然也能与已经加载的程序进行交互。REPL代表读取(Read)、求值(Evaluate)、打印(Print)和 循环(Loop)。为了使用REPL,你只需要使用操作系统提供的命令行来运行它。 表达式求值 打开你的REPL,随机敲入一些字符。很大几率上Clojure会及时地作出相应一个错误。它可不是什么值都会接受。 =>ugh java.lang.Exception: Unable to resolve symbol: ugh in this context.. 实际上你会发现Clojure只能对符合语法规则的表达式求值。 有一点需要记住,所有的表达式都会返回值。即使这个表达式什么也不做,它也会返回值,哪怕仅仅是一个'nil'(类似java中的null)。 =>(do) //先不用管do是做什么的,其实什么也不做 nil 这是一个很好的特性

clojure 新手指南(11):正则表达式

十年热恋 提交于 2019-11-27 16:50:34
接着上篇,继续我们的时间和日期函数的探讨。我们可以定义一个函数,将一个日期字符串分成一个列表。列表元素分别为年、月、日、时、分、秒。为了完成这个字符串分割操作,我们使用“re-split‘函数。re-split函数需要一个正则表达式参数用于确定如何分割字符串。Clojure 依赖java的正则表达式库来处理这些操作。 re-split函数是Clojure Contrib中字符串库的一部分,所以这就需要确保你能访问Clojure Contrib库。你可以用下面这种方式来加载字符串工具库。(注意,记得进入REPL时要加载contrib.jar,忘了 点这 ) => (use 'clojure.contrib.str-utils) nil 一旦加载完库,我们就可以使用re-split了: =>(re-split #" " "2011 06 04 17 50 21") ("2011" "06" "04" "17" "50" "21") =>(class #" ") java.util.regex.Pattern =>(re-split #":" "2011:06:04:17:50:21") ("2011" "06" "04" "17" "50" "21") 我们上面用到的正则表达式非常直白。它们以”#“开头,后面跟着包含需要匹配的正则模式。正则表达式非常复杂,不是本文重点