小编Pau*_*des的帖子

Lisp中的数组与列表:为什么下面的代码中的列表这么快?

在Project Euler中解决问题75时,我得到了意外的结果。我的代码确实找到了正确的解决方案,但是行为却很奇怪。

我的解决方案包括遍历毕达哥拉斯树(Barning's矩阵)直到达到周界极限,计算周界假定每个值的次数,最后计算仅发生一次的周长。我公认的不整洁但有效的代码是:

(defparameter *barning-matrixes*
   '(#(1 -2  2) #(2 -1  2) #(2 -2  3)
     #(1  2  2) #(2  1  2) #(2  2  3)
     #(-1 2  2) #(-2 1  2) #(-2 2  3)))

(defparameter *lengths* (make-array 1500001 :initial-element 0))

(defun expand-node (n)
"Takes a primitive Pythagorean triple in a vector and traverses subsequent nodes in the the tree of primitives until perimeter > 1,500,000"
   (let ((perimeter (reduce #'+ n)))
   (unless (> perimeter 1500000)
     (let ((next-nodes (mapcar …
Run Code Online (Sandbox Code Playgroud)

math common-lisp discrete-mathematics pythagorean

5
推荐指数
1
解决办法
1083
查看次数

为什么函数适用于长列表?

作为一些欧拉苦难的一部分,我正试图用分解轮编码一个Eratosthenes筛子.到目前为止我的代码是:

(defun ring (&rest content)
"Returns a circular list containing the elements in content.
 The returned list starts with the first element of content."
   (setf (cdr (last content)) content))

(defun factorization-wheel (lst)
"Returns a circular list containing a factorization 
 wheel using the list of prime numbers in lst"
   (let ((circumference (apply #'* lst)))
     (loop for i from 1 to circumference
           unless (some #'(lambda (x) (zerop (mod i x))) lst)
             collect i into wheel
           finally …
Run Code Online (Sandbox Code Playgroud)

lisp common-lisp apply sieve-of-eratosthenes wheel-factorization

4
推荐指数
1
解决办法
132
查看次数

lisp宏中的&optional参数:为什么这个变量的行为如下?

我正在尝试创建一个具有&optional默认值参数的lisp宏.遗憾的是,根据是从默认值还是从提供给宏的参数读取参数,对参数的处理方式也不同.下面的代码片段重现了这个问题:

(setf table1 '((1 2 3)
               (4 5 6))
      table2 '((10 20 30)
               (40 50 60))) 

(defmacro test-lambda (f &optional (tableau table1))
   `(list ,f ,tableau))

? (test-lambda 0 table2)   ;; This works...
(0 ((10 20 30) (40 50 60)))

? (test-lambda 0)          ;; ...but this doesn't
> Error: Car of ((1 2 3) (4 5 6)) is not a function name or lambda-expression.
> While executing: CCL::CHEAP-EVAL-IN-ENVIRONMENT, in process listener(1).
> Type :POP to abort, :R for a …
Run Code Online (Sandbox Code Playgroud)

lisp macros common-lisp

3
推荐指数
1
解决办法
149
查看次数

可以控制Lisp的FORMAT函数的隐式转换吗?

在尝试将分数舍入到9位数时,我遇到了以下情况:

? (format t "~9$" 763700091/112000148)
6.818742300
Nil
Run Code Online (Sandbox Code Playgroud)

但舍入仅精确到5位数.我预计format会自动处理这个问题; 也就是说,给定表示它所需的位数,将分数转换为双精度,但显然format不能那样工作.如果你强迫这个分数加倍,你会得到:

? (coerce 763700091/112000148 'double-float)
6.818741802019762D0
Run Code Online (Sandbox Code Playgroud)

是否有控制指令指示format在这种情况下使用双精度.或者我必须double-float在将其传递给格式化之前将其强制转换为完成此操作?

我知道format是非常复杂的,你当然可以通过调用一个外部函数来实现这一点,但我希望通过更简单的调整来实现~9$

lisp format floating-point common-lisp

3
推荐指数
1
解决办法
73
查看次数

如何在不使用`eval`的情况下编写此宏?

我正在尝试编写一个宏,该宏创建任意数量的嵌套循环,并在每次循环迭代时执行一些代码。在我的第一次尝试(如下所示)中,宏返回了代码,而不是运行它。

;; WRONG! Returns a bunch of nested loops instead of evaluating the code.

(defmacro do-combinations ((var lists) &body body)
  `(let* ((lst (mapcar #'(lambda (x)
                           `(loop for ,(gensym) in (list ,@x) do))
                       ,lists))
          (symbols (mapcar #'caddr lst)))
     (reduce #'(lambda (x y) `(,@y ,x))
             lst
             :initial-value `(let ((,',var (list ,@symbols)))
                               (progn ,',@body)))))
CL-USER 25 : 1 > (do-combinations (n '((1 2 3)
                                       (10 20 30)
                                       (100 200 300)))
                   (pprint n))
(LOOP FOR #:G872 IN (LIST 100 200 300)
      DO (LOOP FOR …
Run Code Online (Sandbox Code Playgroud)

macros eval common-lisp

3
推荐指数
1
解决办法
249
查看次数

在 NumPy 中计算 Tp,q,r = Sp,q Sp,r 的最佳方法是什么?

我试图将一系列 m 个输入序列(每个有 n 个项目(s m,n))转换为一个张量,其中 t p,q,r = s p,q s p,r

虽然我的代码确实有效,但我觉得必须有更好的解决方案。这就是我得到的。

# nseqs is the number of sequences
# seq_length is the sequence length
# seq is a list of sequences

output = np.empty((nseqs, seq_length, seq_length))
for n in range(nseqs):
    for i, j in enumerate(seq[n]):
        output[n, i, :] = j
    output[n, :, :] *= output[n, :, :].T
Run Code Online (Sandbox Code Playgroud)

更重要的是,有没有一种方法可以重新调整output张量,以便乘法阶段在单个步骤中完成,而无需这些循环?

python numpy

3
推荐指数
1
解决办法
81
查看次数

为什么在将代码包装到包中之后,列表中的两个元素不再被识别?

defpackage在Lisp中弄湿了脚,开始了一个可耻的开始,即一个我无法理解的错误.

下面的代码是尝试创建子语言以对向量执行中缀操作.我想将它用于涉及一些线性代数的项目.

我的代码的"肉"是parse-infix.此函数查找具有最高优先级的运算符,调用apply-op替换所述运算符及其操作数,operator (operand, operand)从而收缩列表,并迭代直到列表仅包含结果.支持的运算符是四个规则,相等(将结果绑定到Lisp符号)和向量连接.

这是代码,疣和所有:

(defpackage :infix
       (:use :common-lisp)
   (:export operator-list
            operators
            parse-infix
            infix))

(in-package :infix)

(defun parse-input (a)
 "Turns symbols into numbers as necessary while reading an expression"
    (if (symbolp a) (symbol-value a) a))

;; Definition of structure containing data for one operator

(defmacro mapf (op type)
 ""
   `(lambda (a b) 
       (map ,type #'(lambda (x y)
                       (funcall ,op x y)) (parse-input a) (parse-input b))))

(defstruct (operator
              (:conc-name op-)
              (:constructor op …
Run Code Online (Sandbox Code Playgroud)

common-lisp infix-notation package infix-operator

2
推荐指数
1
解决办法
75
查看次数

在Clozure lisp(noob)中避免回声

我正在用lisp弄湿我的脚,并遇到一个(我认为)不寻常的问题.我想创建很长的列表; 即,类似的东西(setf *mat* (make-list 1000000)),但没有Nil在屏幕上打印出一百万次.

我想出的最好的是......

(let () (setf *mat* (make-list 1000000)) (length *mat*))
Run Code Online (Sandbox Code Playgroud)

(或在结束时的一些其他简短但无用的功能)

...但我怀疑有一个更好的解决方案来避免这些sesquipedalian打印输出.任何输入都表示赞赏.顺便说一下,我在Windows 7下使用Clozure v1.10.

lisp common-lisp

1
推荐指数
2
解决办法
78
查看次数

为什么交联的defstructs导致堆栈溢出?

在玩图表时,我得到了一个奇怪的错误,我不太明白.下面的代码重现了这个问题.

;; Define struct to store a node with links to other nodes.
(defstruct node properties links)

;; Make two nodes
(setf a (make-node :properties '(:name a))
      b (make-node :properties '(:name b)))

;; Create link from b to a. This works fine...
(push b (node-links a))

;; ... but this crosslink makes lisp chase its own tail for a while and then crash with a stack overflow.
(push a (node-links b))
Run Code Online (Sandbox Code Playgroud)

我和SBCL和Clozure得到了相同的结果.设置*print-length*为可管理的值不起作用.

所以我的问题是:为什么这段代码不能创建与循环列表相同类型的无限打印循环(即没有堆栈溢出和Ctrl-C可停止).任何输入都表示赞赏.

谢谢,保罗

sbcl common-lisp data-structures clozure-cl

1
推荐指数
1
解决办法
45
查看次数

在Lisp宏的Lambda列表中结合&key和&rest的最佳方法是什么?

我使用宏实现了Heap的算法。它工作正常,但我想对其进行调整,以便它将按需生成回溯或非回溯代码。换句话说,我希望宏要么对其进行排列的序列进行内部复制,要么对宏外部可用的序列进行处理。

我完全无法令人满意的,令人尴尬的代码是:

;; Anaphoric version
;; To make it non-anaphoric, substitute (,var (copy-seq ,vec)) for (,var ,vec)
(defmacro run-permutations (var vec &rest body)
 "Executes body for all permutations of vec, which is stored in variable var"
  `(let ((,var ,vec))
     (labels ((generate (&optional (n (length ,var)))
       (if (= n 1)
         (progn ,@body)
        (progn 
           (loop for i from 0 below (1- n)
                 do (progn 
                      (generate (1- n))
                      (rotatef (aref ,var (if (evenp n) i 0))
                               (aref ,var (1- n)))))
           (generate …
Run Code Online (Sandbox Code Playgroud)

lisp macros common-lisp

0
推荐指数
1
解决办法
169
查看次数

处理参数传递给Common Lisp中的defmacro

在尝试解决Project Euler中的问题时,我编写了以下函数和宏:

(defun digits (n &key (base 10)) ;; Returns a list with the digits of 'n'
   (if (< n base) (list n)       ;; in a given base.
      (multiple-value-bind (div rem)
         (floor n base)
         (concatenate 'list (digits div :base base) (list rem)))))

(defmacro test-palindromes (n1 n2)
   (let* ((dn1 (digits n1)) (dn2 (digits n2))
          (hash (loop for i in dn1 ; A-list describing digit associations
                      collecting (assoc i (pairlis dn2
                            (loop for i from 0 below (length dn1)
                                  collecting i))))))
      `(lambda …
Run Code Online (Sandbox Code Playgroud)

macros lambda common-lisp

0
推荐指数
1
解决办法
81
查看次数