lisp

Transposing lists in Common Lisp

纵饮孤独 提交于 2019-12-17 19:15:56
问题 I am trying to transpose a list of lists; my comments indicate the thought process. (setq thingie '((1 2 3) (4 5 6) (7 8 9))) ;;test case (defun trans (mat) (if (car mat) (let ((top (mapcar 'car mat)) ;;slice the first row off as a list (bottom (mapcar 'cdr mat))) ;;take the rest of the rows (cons top (trans bottom)))) ;;cons the first-row-list with the next-row-list mat) (trans thingie) => ((1 2 3) (4 5 6) (7 8 9)) ;;wait what? But, I really want it to be ((1 4 7) (2 5 8) (3 6 9)) What am I

Python generators in various languages [closed]

£可爱£侵袭症+ 提交于 2019-12-17 17:56:09
问题 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 6 years ago . How do you emulate Python style generators in your favorite language? I found this one in Scheme. It must be interesting to see other

Lisp commenting convention

為{幸葍}努か 提交于 2019-12-17 17:33:23
问题 What is the Lisp convention about how many semicolons to use for different kinds of comments (and what the level of indentation for various numbers of semicolons should be)? Also, is there any convention about when to use semicolon comments and when to use #|multiline comments|# (assuming they exist and exist on multiple implementations)? 回答1: In Common Lisp: ;;;; At the top of source files ;;; Comments at the beginning of the line (defun test (a &optional b) ;; Commends indented along with

Dynamic and Lexical variables in Common Lisp

不问归期 提交于 2019-12-17 17:32:08
问题 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:

Collection of Great Applications and Programs using Macros

泪湿孤枕 提交于 2019-12-17 17:28:19
问题 I am very very interested in Macros and just beginning to understand its true power. Please help me collect some great usage of macro systems. So far I have these constructs: Pattern Matching: Andrew Wright and Bruce Duba. Pattern matching for Scheme, 1995 Relations in the spirit of Prolog: Dorai Sitaram. Programming in schelog. http://www.ccs.neu.edu/home/dorai/schelog/schelog.html Daniel P. Friedman, William E. Byrd, and Oleg Kiselyov. The Reasoned Schemer. The MIT Press, July 2005 Matthias

Alan Kay's Eval/Apply Einstein Moment

江枫思渺然 提交于 2019-12-17 15:44:47
问题 Alan Kay said that reading the code closely and finding the 1 and only bug in the code on page 13 of the Lisp 1.5 manual, helped him understand Computer Science by a factor of 100 better. The code in question is the 1st release of eval & apply that looks anything remotely like modern lisp (that I'm aware of). Since the correct answer is likely known but lost (my google-fu is decent and I've searched for 20 mins at least) I will award the 1st correct answer (I will be looking at edit times so

How do I access the contents of the current region in Emacs Lisp?

≯℡__Kan透↙ 提交于 2019-12-17 15:32:49
问题 I want to access the contents of the current region as a string within a function. For example: (concat "stringa" (get-region-as-string) "stringb") Thanks Ed 回答1: buffer-substring together with region-beginning and region-end can do that. 回答2: As starblue says, (buffer-substring (mark) (point)) returns the contents of the region, if the mark is set. If you do not want the string properties, you can use the 'buffer-substring-no-properties variant. However, if you're writing an interactive

How can I read the contents of a file into a list in Lisp?

可紊 提交于 2019-12-17 11:55:29
问题 I want to read in the contents of a file into a list. Some of my attempts so far have been - (defun get-file (filename) (let ((x (open filename))) (when x (loop for line = (read-line x nil) while line do (list line))) (close x))) (defun get-file (filename) (let ((x (open filename :if-does-not-exist nil)) (contents (list nil))) (when x (loop for line = (read-line x nil) while line do (cons contents line))) (close x) contents)) (defun get-file (filename) (let ((x (open filename :if-does-not

Fast Prime Number Generation in Clojure

大憨熊 提交于 2019-12-17 10:24: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

How to remove nested parentheses in LISP

☆樱花仙子☆ 提交于 2019-12-17 09:53:06
问题 How can I remove nested parentheses recursively in Common LISP Such as (unnest '(a b c (d e) ((f) g))) => (a b c d e f g) (unnest '(a b)) => (a b) (unnest '(() ((((a)))) ())) => (a) Thanks 回答1: Here's what I'd do: (ql:quickload "alexandria") (alexandria:flatten list) That works mainly because I have Quicklisp installed already. 回答2: (defun flatten (l) (cond ((null l) nil) ((atom l) (list l)) (t (loop for a in l appending (flatten a))))) 回答3: I realize this is an old thread, but it is one of