小编Tim*_*ies的帖子

有没有办法混合LISP的宏&optional和&key参数?

我想定义一个LISP宏dolist,让我定义一个可选的输出参数。在下面的案例研究中,此宏将称为doread。它将从文件中读取行并返回以这种方式找到的行数。

(let ((lines 0))
  (doread (line file lines)
     ;; do something with line
     (incf lines)))
Run Code Online (Sandbox Code Playgroud)

问题是要使它lines在上面的宏中起作用

我可以使用&key来完成我想做的事情,但不能使用&optional&key来做(因为我想控制文件的读取方式,所以需要&key;例如,使用reador read-line或其他方法)。

现在下面的作品到作品走错了路。这里的out参数必须是a &key而不是&optional:

;; this way works... 

(defmacro doread ((it f  &key out (take #'read)) &body body)
  "Iterator for running over files or strings."
  (let ((str (gensym)))
    `(with-open-file (,str f)
       (loop for ,it = (funcall ,take ,str nil)
             while ,it do
             (progn ,@body))
       ,out)))

;; lets me define something …
Run Code Online (Sandbox Code Playgroud)

lisp macros common-lisp

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

在Julia中,如何拒绝某个字符的单词?

我想收集包含某个字符的单词。

我觉得它应该比我当前的解决方案(要大量使用正则表达式)容易得多。

但是a,我只是不明白。

这是我当前的解决方案,可以正常工作:

want(s) = match(r"\?",s) == nothing
[s for s in lst if want(s)]
Run Code Online (Sandbox Code Playgroud)

其他所有东西给我语法错误:

[s for s in lst if not '?' in s]
[s for s in lst if not ('?' in s)]
filter((x) ->  not ('?' in x),["?asdas","bbb"])
Run Code Online (Sandbox Code Playgroud)

我可以用一个冗长的三元运算符来做到这一点:

 filter((x) ->  ('?' in x ? false : true),["?asdas","bbb"])
Run Code Online (Sandbox Code Playgroud)

但这似乎并不优雅。

有什么建议吗?

string list-comprehension julia

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

标签 统计

common-lisp ×1

julia ×1

lisp ×1

list-comprehension ×1

macros ×1

string ×1