For loop 如何在Lisp中调用for循环中的函数?
我的文件夹中有25个文本文件要测试。但我不想为25个文件编写代码,这意味着测试这些文件需要25行For loop 如何在Lisp中调用for循环中的函数?,for-loop,lisp,common-lisp,For Loop,Lisp,Common Lisp,我的文件夹中有25个文本文件要测试。但我不想为25个文件编写代码,这意味着测试这些文件需要25行 (myfunc "myfiles/txtfile1.txt") (myfunc "myfiles/txtfile2.txt") ... 如何使用for循环调用此函数?如下面的示例代码。对不起,我的英语很差 (loop for x from 1 to 25 do ((myfunc "myfiles/txtfile~a.txt" x)) ) ) 您可以将文件名末尾的数字与格式连接起来: (从1
(myfunc "myfiles/txtfile1.txt")
(myfunc "myfiles/txtfile2.txt")
...
如何使用for循环调用此函数?如下面的示例代码。对不起,我的英语很差
(loop for x from 1 to 25
do ((myfunc "myfiles/txtfile~a.txt" x))
)
)
您可以将文件名末尾的数字与格式连接起来: (从1到25的x循环
do(myfunc(格式为nil“myfiles/txtfile~a.txt”x))一个简单的方法是使用
格式的函数():
由于所有文件都在一个目录中,因此您还可以查看目录
预定义函数(,)。一些函数方法
(mapcar function (directory (merge-pathnames wildcard path)))
mapcar
-将函数
应用于目录
生成的参数列表<代码>函数
只能接受一个参数
工作示例
61650326.lisp
并运行代码段
~$: sbcl --script 61650326.lisp
PATHNAME
PATHNAME
PATHNAME
PATHNAME
PATHNAME
PATHNAME
PATHNAME
PATHNAME
PATHNAME
PATHNAME
`do((myfunc“myfiles/txtfile~a.txt”x))`注意,下面的两个括号是错误的。这里有一个关于格式
指令的入门:实际上,这正是我想要做的,但我对Lisp是新手。我的路径是“user/myfiles/textfile.txt”。你能给我一个这样的例子吗?我如何读取这个文件夹中的所有文件?@JuanRodre我用更明确的例子扩展了答案非常感谢@Hellseher
;;;; File : 61650326.lisp
;;;; Modified : <2020-5-08 Fri 08:51:12 BST>
;;;; URL : https://stackoverflow.com/questions/61650326
(defparameter *user-path* "t/user/myfiles/")
(defparameter *wildcard* "*.txt")
(defparameter *function* 'type-of)
(defun map-on-files (path wildcard function)
"Map FUNCTION on a list of files found under PATH WILDCARD."
(mapcar function (directory (merge-pathnames wildcard path))))
(format t "~{~A~%~}" (map-on-files *user-path* *wildcard* *function*))
~$: mkdir -p t/user/myfiles
~$: for i in $(seq 10); do mktemp -p t/user/myfiles/ --suffix=.txt; done
t/user/myfiles/tmp.S4wKkJ85sq.txt
t/user/myfiles/tmp.j3XeT2hneq.txt
t/user/myfiles/tmp.mut6GLhlaT.txt
t/user/myfiles/tmp.1l0I5oGDaR.txt
t/user/myfiles/tmp.hBpvOfarye.txt
t/user/myfiles/tmp.m1WfmZxrU8.txt
t/user/myfiles/tmp.o6QbvjXMh9.txt
t/user/myfiles/tmp.6CmPvWf7GO.txt
t/user/myfiles/tmp.ZNWcaymY0g.txt
t/user/myfiles/tmp.JfUQ4cW0dD.txt
~$: sbcl --script 61650326.lisp
PATHNAME
PATHNAME
PATHNAME
PATHNAME
PATHNAME
PATHNAME
PATHNAME
PATHNAME
PATHNAME
PATHNAME