lisp

clojure 新手指南(6):全局绑定&匿名函数

佐手、 提交于 2019-12-01 06:00:51
绑定变量 在Clojure中,我们可以使用 " def " 给函数和数据赋予一个名字。例如我们定义一个叫做“alphabet”的字符串变量 user=> (def alphabet "abcdefghijklmnopqrstuvwxyz") #'user/alphabet user=> alphabet "abcdefghigklmnopqrstuvwxyz" 使用变量的好处一个是方便使用,另一个在于一个有意义的名字大大增强代码的可读性。举个例子,如果我们想要知道一个字符串的包含的字符数量: user=> (count alphabet) 26 我们就不需要在每次去计算字符串数量的时候都要写下长长的一大串了。只用把与该字符串绑定的变量名传过去就行。使用变量绑定,我们不仅能使代码具有良好的可读性,有时候还能增加代码性能。 增加性能?这个很好理解也很常用。例如我们经常把计算好的值绑定到一个变量上。这样我们在用到这个值的时候就不用每次都得重新计算了。对于一些费时的操作,这可能省下大量的时间。下面的例子就说明了这一点: user=> (def alphabet-length (count alphabet)) user=> alphabet-length 26 如果我们要计算字符串长度的两倍,就可以直接使用绑定的字符串长度: user=> (+ alphabet-length

How to convert a string to list using clisp?

蓝咒 提交于 2019-12-01 04:53:00
How can i convert the string "1 2 3 4 5 6 7" into the list (1 2 3 4 5 6 7) elegantly? I am using CLISP. Hint: Take a look at with-input-from-string . You should use parse-integer in a loop. For example, using loop : (let ((string "1 2 3")) (loop :for (integer position) := (multiple-value-list (parse-integer string :start (or position 0) :junk-allowed t)) :while integer :collect integer)) ⇒ (1 2 3) If you need better control about the splitting, use the split-sequence or cl-ppcre library. If you need to parse more general number formats, use the parse-number library. Libraries are available

Print output into a file or not print output?

纵然是瞬间 提交于 2019-12-01 04:32:36
问题 I'd like to save or ignore outputs when I execute a specific function in lisp. I use Emacs and CCL. For example, (defun foo (x) (format t "x = ~s~%" x)) and if I execute the function, it prints out "x = 5". But I don't want to printout in a buffer, because if I have a large amount of iteration, the speed of simulation will be decreased. Any idea? 回答1: You can temporarily redirect standard output by binding *standard-output* to a stream. For example, a broadcast stream with no output streams

Variable passed to macro gets resolved in wrong namespace?

笑着哭i 提交于 2019-12-01 04:17:18
The Noir macro defpage is giving me a little bit of trouble. I am trying to construct a call similar to this: (defpage [:post "some/url"] [data] ;; some stuff... ) However, instead of using the keyword :post I would like to use a variable, like this: (def my-method :post) (defpage [my-method "some/url"] [data] ;; some stuff... ) The problem is that when the macro expands, it wants to resolve the variable my-method in the compojure.core namespace instead of my own, giving me the error: No such var: compojure.core/MY-METHOD How can I force my-method to resolve in the current context? Michiel

What does backtick mean in LISP?

。_饼干妹妹 提交于 2019-12-01 04:16:16
I have this macro, which rewrites define. If I remove the " ` " backtick it won't work. What is the explanation? (defmacro define ((name &rest r) body) `(defun ,name ,r ,body)) A single quote followed by the written representation of a value will produce that value: Example: '(1 x "foo") will produce a value that prints as (1 x "foo") . Suppose now that I don't want a literal symbol x in the list. I have a variable x in my program, and I want to insert the value to which x . To mark that I want the value of x rather than the symbol x , I insert a comma before x : '(1 ,x "foo") It won't work as

Calling function from macro inside Quicklisp package

試著忘記壹切 提交于 2019-12-01 04:08:38
问题 I put failing.asd (in-package :asdf-user) (defsystem "failing" :description "some code destined to fail" :version "0.1" :author "me" :components ((:file "package"))) and package.lisp (defpackage :failing (:export :foo :bar)) (in-package :failing) (defun foo () 42) (defmacro bar () (let ((x (foo))) `(print ,x))) (bar) into ~/quicklisp/local-projects/failing . Using Clozure CL with Quicklisp installed, I run (ql:quickload :failing) which gives me To load "failing": Load 1 ASDF system: failing ;

LISP cons in python

China☆狼群 提交于 2019-12-01 03:49:54
Is there an equivalent of cons in Python? (any version above 2.5) If so, is it built in? Or do I need easy_install do get a module? In Python, it's more typical to use the array-based list class than Lisp-style linked lists. But it's not too hard to convert between them: def cons(seq): result = None for item in reversed(seq): result = (item, result) return result def iter_cons(seq): while seq is not None: car, cdr = seq yield car seq = cdr >>> cons([1, 2, 3, 4, 5, 6]) (1, (2, (3, (4, (5, (6, None)))))) >>> iter_cons(_) <generator object uncons at 0x00000000024D7090> >>> list(_) [1, 2, 3, 4, 5,

Does Common Lisp have a something like java's Set Interface/implementing classes?

风流意气都作罢 提交于 2019-12-01 03:35:38
I need something like this , a collection of elements which contains no duplicates of any element. Does Common Lisp, specifically SBCL, have any thing like this? For a quick solution, just use hash tables, as has been mentioned before. However, if you prefer a more principled approach, you can take a look at FSet , which is “a functional set-theoretic collections library”. Among others, it contains classes and operations for sets and bags. (EDIT:) The cleanest way would probably be to define your set-oriented operations as generic functions. A set of generic functions is basically equivalent

Suppress warning for function arguments not used in LISP

白昼怎懂夜的黑 提交于 2019-12-01 03:30:47
问题 In lisp, I need to define a set of functions, all with the same number of arguments. However, the functions may or may not use all the arguments, leading to a spur of warning messages. For example: (defun true (X Y) X) [...] ; caught STYLE-WARNING: ; The variable Y is defined but never used. Is there a way to warn the compiler that is was intended? 回答1: See the Common Lisp Hyperspec: Declaration IGNORE, IGNORABLE (defun true (X Y) (declare (ignore y)) X) 来源: https://stackoverflow.com

What does the asterisk do in Python other than multiplication and exponentiation? [duplicate]

别说谁变了你拦得住时间么 提交于 2019-12-01 03:19:03
问题 This question already has answers here : asterisk in function call (3 answers) proper name for python * operator? (7 answers) Closed 6 years ago . In Peter Norvig's Lisp interpreter written in Python (http://norvig.com/lispy.html), he defines Lisp's eval as follows: def eval(x, env=global_env): "Evaluate an expression in an environment." if isa(x, Symbol): # variable reference return env.find(x)[x] elif not isa(x, list): # constant literal return x elif x[0] == 'quote': # (quote exp) (_, exp)