我需要一个函数,该函数将接收一个包含单词的列表,并将该列表拆分为两个列表,如果在任何时候找到单词“FOO”。我想出了一个递归解决方案,可能不是最好的,但我遇到了一些麻烦。我只需要传递 1 个参数,即要分析的列表,但我不知道如何构建第二个列表。有什么建议么?谢谢!
;Splits a list into 2 if the word 'FOO' is present
;----------------------------------------------------------------------
;LOAD FILE: (load "C:\\split.lisp")
;USAGE: (split '(with great power foo comes great responsibility) '())
;OUTPUT: ((with great power)(comes great responsibility))
(defun split (x y)
(cond
( ;IF: first element in list is nil
(EQ (car x) nil)
x ;RETURN the list
)
( ;ELSE IF: first element is 'FOO'
(EQ (car x) 'FOO)
(cons (reverse y ) (cons (cdr x) nil))
)
( ;ELSE: recursively call split but pass the rest of x and
;prepend y with the head of x
t
(split (cdr x) (cons (car x) y))
)
) ;END cond
) ;END split