Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lisp 如何将两个相似的函数合并为一个函数_Lisp_Common Lisp - Fatal编程技术网

Lisp 如何将两个相似的函数合并为一个函数

Lisp 如何将两个相似的函数合并为一个函数,lisp,common-lisp,Lisp,Common Lisp,我有两个相似的函数,它们可以完美地工作。但我想把它们组合成一个函数。求你了,我需要你的帮助。谢谢大家! 职能1 (defun read_char_into_list (&aux (list nil)) (setq char ; link this variable with open the file (open "fichier-char.txt" ; open the file :di

我有两个相似的函数,它们可以完美地工作。但我想把它们组合成一个函数。求你了,我需要你的帮助。谢谢大家!

职能1

(defun  read_char_into_list (&aux (list nil)) 
    (setq char                     ; link this variable with open the file
        (open "fichier-char.txt"     ; open the file
            :direction :input :if-does-not-exist :error)) 
    (loop 
        (cond 
            ((not (setq line (read-line char nil nil))) (return)) 
            ((push (read-from-string line) list)) ) ) 
    (close char)    
    (reverse list) )  

(read_char_into_list) ==> (... e è é ê ë F f ...) 
职能2

(defun  read_code_into_list (&aux (list nil)) 
    (setq code 
        (open "fichier-code.txt" 
            :direction :input :if-does-not-exist :error)) 
    (loop 
        (cond 
            ((not (setq line (read-line code nil nil))) (return)) 
            ((push (read-from-string line) list)) ) ) 
    (close code)    
    (reverse list) ) 

(read_code_into_list) ==> (...65 192 194 196 198 ...)
功能联合

(defun  read-into-list (fichier &aux (list nil) )
    (setq fichier 'code or 'char)  
    (setq code
        (open (string-concat "alphabet_"   (string 'code)  ".txt")
            :direction :input :if-does-not-exist :error)
    (setq char
        (open (string-concat "alphabet_"   (string 'char)  ".txt")
            :direction :input :if-does-not-exist :error) ) 
    (loop
        (cond
            ((not (setq line (read-line (or code char) nil nil)))        (return))  
            ((push (read-from-string line) list)) ) )
    (close code) 
    (close char)   
    (reverse list) ) )

(read-into-list 'code) ==> should give (...65 192 194 196 198 ...)
(read-into-list 'char) ==> should give (... e è é ê ë F f ...)

正如我所读到的,这两个函数之间的区别仅在于 他们从中读取的文件的名称,所以您只需传递名称 将文件的名称添加到函数:

(defun read-into-list (file-name)
  (with-open-file (in file-name) ; :direction defaults to :input
    (loop :for line = (read-line in nil nil)
      :while line
      :collect (read-from-string line))))
并称之为

(read-into-list "fichier-char.txt")

为了满足您的通话要求,您可以

(defun read-into-list-1 (what)
  (read-into-list (concatenate 'string "fichier-" (string what) ".txt")))
请注意,可能会返回:


另外请注意,即使出现错误,也要确保关闭文件,例如。

最后我找到了方法。我的问题是如何选择要打开的文件。我只是通过一个条件提到它,第一个条件:如果variable='code,那么打开这个文件,如果variable='char,那么打开那个文件。就这样


谢谢你试图帮助我,也许下次你会的。

谢谢你对我的案子感兴趣。我有义务保持相同的结构,并像这样调用函数:(read-into-list'code)或(read-into-list'char)@user3653521如果
fichier
是您的参数,您可以使用文件名
(如果(eq-fichier'char)“fichier-char.txt”“fichier-code.txt”)
(defun read-into-list-1 (what)
  (read-into-list (concatenate 'string "fichier-" (string what) ".txt")))
(string 'code)
==> "CODE"