lisp

How do I find the index of an element in a list in Racket?

ⅰ亾dé卋堺 提交于 2019-11-27 13:47:08
This is trivial implement of course, but I feel there is certainly something built in to Racket that does this. Am I correct in that intuition, and if so, what is function? Strangely, there isn't a built-in procedure in Racket for finding the 0-based index of an element in a list (the opposite procedure does exist, it's called list-ref ). However, it's not hard to implement efficiently: (define (index-of lst ele) (let loop ((lst lst) (idx 0)) (cond ((empty? lst) #f) ((equal? (first lst) ele) idx) (else (loop (rest lst) (add1 idx)))))) But there is a similar procedure in srfi/1 , it's called

Why do we need funcall in Lisp?

巧了我就是萌 提交于 2019-11-27 13:39:31
Why do we have to use funcall to call higher order functions in Common Lisp? For example, why do we have to use: (defun foo (test-func args) (funcall test-func args)) instead of the simpler: (defun bar (test-func args) (test-func args)) Coming from a procedural background, I'm a bit surprised by that since the languages I'm more used to (e.g. Python, C#) don't need the distinction. In particular, on the source level at least, the C# compiler transforms it to something like func.invoke() . The only problem I see is that this would mean we couldn't call a global function test-func anymore

Emacs :TODO indicator at left side

我怕爱的太早我们不能终老 提交于 2019-11-27 12:57:58
问题 I want to have sort of indiacator at left side of the line wherever I have in the source code #TODO : some comment //TODO: some comments The indicator could be a just mark and I already enabled line numbers displayed at emacs. 回答1: This command will do something like you want. (defun annotate-todo () "put fringe marker on TODO: lines in the curent buffer" (interactive) (save-excursion (goto-char (point-min)) (while (re-search-forward "TODO:" nil t) (let ((overlay (make-overlay (- (point) 5)

Practical use of curried functions?

痴心易碎 提交于 2019-11-27 12:49:30
问题 There are tons of tutorials on how to curry functions, and as many questions here at stackoverflow. However, after reading The Little Schemer, several books, tutorials, blog posts, and stackoverflow threads I still don't know the answer to the simple question: "What's the point of currying?" I do understand how to curry a function, just not the "why?" behind it. Could someone please explain to me the practical uses of curried functions (outside of languages that only allow one argument per

Mathematica: Unevaluated vs Defer vs Hold vs HoldForm vs HoldAllComplete vs etc etc

心不动则不痛 提交于 2019-11-27 12:23:13
I'm bewildered by all the built-in Mathematica functions that purport to prevent evaluation in some way: Unevaluated , Defer , Hold , and over half a dozen of the form Hold* . The Mathematica documentation just explains each function in isolation without explaining why you would choose one or the other. Can anyone offer a coherent explanation of all these functions? The whole thing seems like a convoluted mess to me. Relating it all to Lisp macros might be a good place to start. Most of the Mathematica language is amazingly well-designed but it seems like Wolfram really painted himself into a

Executing a shell command from Common Lisp

[亡魂溺海] 提交于 2019-11-27 12:11:33
问题 How can i execute a shell (bash) command within a Common Lisp program and assign the output to a variable? 回答1: ASDF provides a RUN-SHELL-COMMAND that works with many Common Lisp implementations including ABCL, Allegro CL, CLISP, Clozure CL, ECL, GCL, LispWorks, SBCL, CMU, XCL and SCL. It takes a control string and a list of arguments like FORMAT , and synchronously executes the result using a Bourne-compatible shell. Capture output by binding an optional stream. 回答2: ITA has released

What exactly is a symbol in lisp/scheme?

谁说我不能喝 提交于 2019-11-27 12:02:52
For the love of the almighty I have yet to understand the purpose of the symbol 'iamasymbol . I understand numbers, booleans, strings... variables. But symbols are just too much for my little imperative-thinking mind to take. What exactly do I use them for? How are they supposed to be used in a program? My grasp of this concept is just fail. In Scheme and Racket, a symbol is like an immutable string that happens to be interned so that symbols can be compared with eq? (fast, essentially pointer comparison). Symbols and strings are separate data types. One use for symbols is lightweight

How is Lisp dynamic and compiled?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 11:32:41
问题 I don't understand how Lisp can be compiled and dynamic. For a language to be able to manipulate and modify and generate code, isn't it a requirement to be interpreted? Is it possible for a language to be completely compiled and still be dynamic? Or am I missing something? What is Lisp doing that allows it to be both compiled and dynamic? 回答1: Lisp is a wide family of language and implementations. Dynamic in the context of Lisp means that the code has a certain flexibility at runtime. It can

Is ECMAScript really a dialect of Lisp?

做~自己de王妃 提交于 2019-11-27 11:27:08
问题 A friend of mine drew my attention the welcome message of 4th European Lisp Symposium: ... implementation and application of any of the Lisp dialects, including Common Lisp, Scheme, Emacs Lisp, AutoLisp, ISLISP, Dylan, Clojure, ACL2, ECMAScript , ... and then asked if ECMAScript is really a dialect of Lisp. Can it really be considered so? Why? Is there a well defined and clear-cut set of criteria to help us detect whether a language is a dialect of Lisp? Or is being a dialect taken in a very

Fast Prime Number Generation in Clojure

白昼怎懂夜的黑 提交于 2019-11-27 11:18:54
I've been working on solving Project Euler problems in Clojure to get better, and I've already run into prime number generation a couple of times. My problem is that it is just taking way too long. I was hoping someone could help me find an efficient way to do this in a Clojure-y way. When I fist did this, I brute-forced it. That was easy to do. But calculating 10001 prime numbers took 2 minutes this way on a Xeon 2.33GHz, too long for the rules, and too long in general. Here was the algorithm: (defn next-prime-slow "Find the next prime number, checking against our already existing list" (