lisp

Lisp: can a macro be recursive?

Deadly 提交于 2019-12-20 21:58:05
问题 I've recently started coding in Lisp, and have already been most impressed with macros - they allowed me to do complex loop-unrolling at compile-time, something I can't do this elegantly in any other language that I know of (i.e. code-generate while keeping the original structure). On to optimization: I've sprinkled type annotations (lots of "the fixnum") in the same code. Once I've added 3 or 4 of them, I realized I was doing it wrong - this is what macros are for, Dont Repeat Yourself... ;

What is the correct term for the following functional programming pattern?

余生长醉 提交于 2019-12-20 19:42:09
问题 I've heard it referred to as a stream, as an infinite list, and sometimes even as a lazy sequence. What is the correct term for the following pattern? (Clojure code shown) (def first$ first) (defn second$ [str] (cond (empty? str) () true ((first (rest str))))) (defn stream-builder [next_ n] (cons n (cons (fn [] (stream-builder next_ (next_ n))) ()))) (defn stream [str n] (cond (= 0 n) () true (cons (first$ str) (stream (second$ str) (- n 1))))) (def odd (stream-builder (fn [n] (+ 2 n))1))

Why are uninterned symbols used for package names and exports in Common Lisp?

心已入冬 提交于 2019-12-20 17:48:06
问题 In a screen cast on Common List the author uses uninterned symbols for package names and exports. (defpackage #:foo (:use :cl) (:export #:bar #:baz)) (in-package #:foo) He also uses the sharp sign in front of anonymous functions. (defun transposed (m) (make-instance 'matrix :rows (matrix-cols m) :cols (matrix-rows m) :generator #'(lambda (i j) (matrix-at m j i)))) In the book Practical Common Lisp the sharp sign isn't used for package names and exports as far as I have read. What's the reason

Do you know of a language with Static Type checking where Code is Data? [closed]

丶灬走出姿态 提交于 2019-12-20 17:39:06
问题 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 2 years ago . Can you name languages with static type checking (like Java) and where code is data (like in LISP)? I mean both things in one language. 回答1: Qi is a statically-typed Lisp dialect. Also, many other Lisp dialects have (optional) static typing. Java itself has very limited

Help me write a Clojure macro which automatically adds metadata to a function definition

天涯浪子 提交于 2019-12-20 17:29:56
问题 I realize that the first rule of Macro Club is Don't Use Macros, so the following question is intended more as an exercise in learning Clojure than anything else (I realize this isn't necessarily the best use of macros). I want to write a simple macro which acts as a wrapper around a regular (defn) macro and winds up adding some metadata to the defined function. So I'd like to have something like this: (defn-plus f [x] (inc x)) ...expand out to something like this: (defn #^{:special-metadata

Is it possible to have an alias for the function name in Lisp?

纵饮孤独 提交于 2019-12-20 12:34:47
问题 ...just like packages do. I use Emacs (maybe, it can offer some kind of solution). For example (defun the-very-very-long-but-good-name () ...) is not to useful later in code. But the name like Fn-15 or the first letters abbreviation is not useful too. Is it possible either to have an alias like for packages or to access the documentation string while trying to recall the function's name? In other words, is it possible for functions to mix somehow self-documenting and short names? 回答1: You

Are there ruby equivalents to car, cdr, and cons?

為{幸葍}努か 提交于 2019-12-20 12:06:27
问题 Are there ruby equivalents to the lisp car, cdr, and cons functions? For those unfamiliar with lisp, here's what I want from ruby: [1,2,3].car => 1 [1,2,3].cdr => [2,3] [2,3].cons(1) => [1,2,3] (in lisp): (car '(1 2 3)) => 1 (cdr '(1 2 3)) => (2 3) (cons 1 '(2 3)) => (1 2 3) 回答1: Ruby arrays are not implemented as singly-linked lists, so it is not as useful to have car and cdr and stuff. If you really wanted, you could do [1,2,3][0] => 1 [1,2,3].first => 1 [1,2,3][1..-1] => [2,3] [1] + [2,3]

Emacs lisp “shell-command-on-region”

旧巷老猫 提交于 2019-12-20 10:25:34
问题 In GNU Emacs, I want to run a program, figlet, on the currently selected text. I then want to comment the region which is produced. I have figured out how to do it using the standard Emacs commands: set mark with C-<space> at the start of the word move cursor to the end of the word C-u M-x shell-command-on-region RET figlet RET M-x comment-region RET However, I have failed to work out how to write an Emacs lisp program to do all this. Here is my attempt: (defun figlet-region () (interactive)

Fixing Lisp Syntax

核能气质少年 提交于 2019-12-20 09:56:03
问题 Being a newbie to Lisp I'm wondering if the Lisp syntax could be "fixed"? Some people say the syntax in Lisp is one of its biggest strengths. I don't quite understand this. Isn't it possible to replace "obvious" parentheses with a combination of white spaces, new lines and indenting? Just like in Python? It looks to me like parentheses are the most used characters in Lisp code. I'm wondering if that's true - but if it is, isn't this a suggestion, that there is some redundancy in the syntax?

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

孤街浪徒 提交于 2019-12-20 09:14:24
问题 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