lisp

Setting SLIME in emacs

試著忘記壹切 提交于 2019-11-28 07:41:40
I was trying to install SLIME. I downloaded the zipped package and according to the README file, I have to put this piece of code in my Emacs configuration file: (add-to-list 'load-path "~/hacking/lisp/slime/") ; your SLIME directory (setq inferior-lisp-program "/opt/sbcl/bin/sbcl") ; your Lisp system (require 'slime) (slime-setup) Setting the SLIME directory is straightforward, but what about the Lisp "system"? How do I find it? Some Linuxes come with CMUCL preinstalled, but since you seem to want to use SBCL, you would need to install it. In terminal, or in Emacs M-x shell . If you are using

How does `if` not evaluate all its arguments?

末鹿安然 提交于 2019-11-28 07:17:46
问题 I'm trying to learn and understand the Lisp programming language to a deep level. The function + evaluates its arguments in applicative order: (+ 1 (+ 1 2)) (+ 1 2) will be evaluated and then (+ 1 3) will be evaluated, but the if function works differently: (if (> 1 2) (not-defined 1 2) 1) As the form (not-defined 1 2) isn't evaluated, the program doesn't break. How can the same syntax lead to different argument evaluation? How is the if function defined so that its arguments aren't evaluated

How to generate all the permutations of elements in a list one at a time in Lisp?

扶醉桌前 提交于 2019-11-28 07:11:23
问题 I already have the code to generate all the permutations for a list of elements. However, I realized that if I want to manipulate the lists that are generated, I would need to traverse this list. This list can be potentially massive and therefore expensive to keep. I wanted to know if there was a way to generate the permutations by each call so that I can check if the list matches with what I need and if not I will generate the next permutation. (Each time the function will return a list one

what is the 'cons' to add an item to the end of the list?

社会主义新天地 提交于 2019-11-28 06:59:16
what's the typical way to add an item to the end of the list? I have a list (1 2 3) and want to add 4 to it (where 4 is the result of an evaluation (+ 2 2)) (setf nlist '(1 2 3)) (append nlist (+ 2 2)) This says that append expects a list, not a number. How would I accomplish this? You could use append , but beware that it can lead to bad performance if used in a loop or on very long lists. (append '(1 2 3) (list (+ 2 2))) If performance is important, the usual idiom is building lists by prepending (using cons ), then reverse (or nreverse ). If the "cons at the front, finish by reversing"

Lisp grammar in yacc

 ̄綄美尐妖づ 提交于 2019-11-28 06:24:56
I am trying to build a Lisp grammar. Easy, right? Apparently not. I present these inputs and receive errors... ( 1 1) 23 23 23 ui ui This is the grammar... %% sexpr: atom {printf("matched sexpr\n");} | list ; list: '(' members ')' {printf("matched list\n");} | '('')' {printf("matched empty list\n");} ; members: sexpr {printf("members 1\n");} | sexpr members {printf("members 2\n");} ; atom: ID {printf("ID\n");} | NUM {printf("NUM\n");} | STR {printf("STR\n");} ; %% As near as I can tell, I need a single non-terminal defined as a program, upon which the whole parse tree can hang. But I tried it

Python generators in various languages [closed]

∥☆過路亽.° 提交于 2019-11-28 05:56:36
How do you emulate Python style generators in your favorite language? I found this one in Scheme. It must be interesting to see other implementations, especially in those languages that don't have first-class continuations. Here is an example in C++ that simulates generators using fibers: Yield Return Iterator for Native C++ Using Fibers The "yield return" iterator is a language feature that was created for one reason: simplicity. It is generally much easier to iterate across whole collectionl, storing all context needed in local variables, rather than crafting a complicated, custom iterator

Why can't tail calls be optimized in JVM-based Lisps?

时光怂恿深爱的人放手 提交于 2019-11-28 05:24:56
Main question: I view the most significant application of tail call optimization (TCO) as a translation of a recursive call into a loop (in cases in which the recursive call has a certain form). More precisely, when translated into a machine language, this would usually be translation into some sort of series of jumps. Some Common Lisp and Scheme compilers that compile to native code (e.g. SBCL) can identify tail-recursive code and perform this translation. JVM-based Lisps such as Clojure and ABCL have trouble doing this. What is it about the JVM as a machine that prevents or makes this

Proxies / delegates in Scala

扶醉桌前 提交于 2019-11-28 04:34:31
I've seen several Scala questions recently (e.g. here , here , and here ) that called for the use of proxies, and it's come up more than once in my own work. The Scala library has a number of proxy traits (14, if I counted correctly). Proxy classes/traits usually contain lots of boilerplate: class FooProxy(val self: Foo) extends Foo { // added behavior def mymethod = ... // forwarding methods def method1 = self.method1 def method2(arg: String) = self.method2(arg) ... } trait Foo { def method1: Unit def method2(arg: String): Unit } My first thought was to define a Proxy[T] trait that could be

Clojure head retention

十年热恋 提交于 2019-11-28 03:58:54
I'm reading Clojure Programming book by O'Reilly.. I came over an example of head retention. First example retains reference to d (I presume), so it doesnt get garbage collected: (let [[t d] (split-with #(< % 12) (range 1e8))] [(count d) (count t)]) ;= #<OutOfMemoryError java.lang.OutOfMemoryError: Java heap space> While second example doesnt retain it, so it goes with no problem: (let [[t d] (split-with #(< % 12) (range 1e8))] [(count t) (count d)]) ;= [12 99999988] What I don't get here is what exactly is retained in which case and why. If I try to return just [(count d)] , like this: (let [

Dynamic and Lexical variables in Common Lisp

老子叫甜甜 提交于 2019-11-28 03:56:17
I am reading the book 'Practical Common Lisp' by Peter Seibel. In Chapter 6, "Variables" sections "Lexical Variables and Closures" and "Dynamic, a.k.a. Special, Variables". http://www.gigamonkeys.com/book/variables.html My problem is that the examples in both sections show how (let ...) can shadow global variables and doesn't really tell the difference between the Dynamic and Lexical vars. I understand how closures work but I don't really get whats so special about let in this example: (defvar *x* 10) (defun foo () (format t "Before assignment~18tX: ~d~%" *x*) (setf *x* (+ 1 *x*)) (format t