【问题标题】:ELISP: Function to prompt user for number and ask user for that number of strings and insert then into listELISP:提示用户输入数字并询问用户该数量的字符串然后插入列表的功能
【发布时间】:2013-02-19 05:39:09
【问题描述】:

我想创建一个 ELISP 函数,它会提示用户输入数字 n,然后不断提示用户输入字符串 n 次。理想情况下,我希望将所有这些字符串放入一个列表中。这是我到目前为止所拥有的。显然,我所拥有的东西不起作用,但它可能有助于澄清我想做的事情的类型。

(defun prompt-user-n-times (n)
  "Prompt user n time for strings and append strings to list"
  (interactive "nHow many strings: ")

  (while (> n 0)
    (append newlist (interactive "sGive me input: "))
    (setq n (- n 1))
))

谢谢。

【问题讨论】:

    标签: function emacs elisp user-input


    【解决方案1】:

    只需为您的新列表定义一个绑定:

    (defun prompt-user-n-times (n)
      "Prompt user n time for strings and append strings to list"
      (interactive "nHow many strings: ")
    
      (let ((newlist ()))
        (while (> n 0)
          (setq newlist (append newlist (list (read-string "Give me input: "))))
          (setq n (- n 1)))
        newlist))
    

    几点说明:interactive 只是在defun 的开头,在 功能,一用其他提示功能,喜欢简单的 read-stringappend 请求两个列表,所以返回的字符串 read-string 的作者应该被 list 函数放入一个列表中

    【讨论】:

    • 请注意,append 在某种程度上是一种反模式,因为它每次都会遍历列表。标准成语是:(let ((newlist nil)) (dotimes (i n) (push (read-from-minibuffer ...) newlist)) (nreverse newlist))。或者,如果你不反对loop(loop repeat n collect (read-from-minibuffer ...))
    猜你喜欢
    • 1970-01-01
    • 2014-12-08
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多