【问题标题】:How to create an array in LISP with user input?如何使用用户输入在 LISP 中创建数组?
【发布时间】:2013-03-14 00:30:45
【问题描述】:

我正在为大学课程学习 eLISP,但我在一个项目中遇到了一些麻烦。我正在尝试编写一个采用列表和大小的方法,然后用用户输入填充该列表。我无法让 eLISP 实际请求输入——出于某种原因,交互式调用无法正常工作。请注意,我使用的是“Array”而不是“List”,因为这就是我编写其他 3 个脚本的方式,现在我太困惑了,无法更改它。

这是我的代码:

(defun readArray(anArray size)
  (if (>= size 0)
      (progn
        (setq value 0)
        (princ "Enter values maybe?\n") ;;note this line is executed,so I think the prog is working
        (interactive "\nnEnter a value: ")
        (setq anArray (list value (readArray (- size 1)))))))

Running (readArray 4) 给了我以下输出:

Enter values maybe?
Enter values maybe?
Enter values maybe?
Enter values maybe?
Enter values maybe?
(0 (0 (0 (0 ...))))

【问题讨论】:

  • 列表,而不是数组。无论如何,“不工作”是非常具有描述性的,但您可能需要对结果做一些事情
  • 列表,是的...这是漫长的一周。另外,“做某事”是什么意思?
  • anArray 参数的用途是什么?你没有使用它的价值。
  • 老实说,我不知道如何用空白参数调用它。该课程不是最全面的。这是一个非常非常广泛的介绍。
  • 它唯一需要的参数是大小,它会自己创建结果并返回。

标签: arrays input elisp interactive


【解决方案1】:

试试这个:

(defun read-list (size)
  (if (> size 0)
      (let ((value (read-from-minibuffer "Enter value maybe? " nil nil t)))
        (cons value (read-list (- size 1))))))

read-from-minibuffer 打印提示并读取用户的响应。

【讨论】:

  • 像魅力一样工作。非常感谢您的宝贵时间。
猜你喜欢
  • 2014-11-06
  • 1970-01-01
  • 2021-09-05
  • 1970-01-01
  • 2017-06-16
  • 2015-08-14
  • 1970-01-01
  • 2016-01-10
  • 1970-01-01
相关资源
最近更新 更多