小编Fat*_*kes的帖子

如何返回特定列表

我正在尝试用LISP编写此代码,但遇到了很多麻烦。

这是我目前所拥有的:

(defun list_incr (x)
    (if (eq (first x) nil)
        x
        (if (< (first x) (first(rest x)))
            (append (list (first x) (first(rest x))) (list_incr (rest(rest x))))
            (list_incr (cons (first x) (rest(rest x)))))))
Run Code Online (Sandbox Code Playgroud)

如果给出列表(1 3 2 4),则需要返回(1 3 4)。目前,我的代码对连续两个递增的数字都可以用,但是当它减小时就不再起作用了。

我需要代码从给定列表返回数量递增的列表。

一些例子:

如果给出列表(1 2 4 6 5),则应返回(1 2 4 6)

如果给出(3 1 4 5 2 6),则应返回(3 4 5 6)

谢谢!

lisp common-lisp

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

如何将字符串拆分为特定的关键字?

我正在尝试将字符串拆分为特定的关键字。我有一个关键字/字符列表。

例如:我有一个关键字列表 {'1', '2', '3', '4', '5', 'let', 'while'}

而且我有一个字符串 let2while4

我想输出一个包含以下内容的列表 {'let', '2', while', '4'}

这可能吗?我目前仅使用带有分隔符''

谢谢!

编辑:下面使用吉尔奇的答案适用于以下示例,但是当我输入完整的关键字时,出现以下错误:

Traceback (most recent call last):
File "parser.py", line 14, in <module>
list = re.findall(f"({'|'.join(keywords)})", input)
File "/usr/lib/python3.7/re.py", line 223, in findall
File "/usr/lib/python3.7/sre_parse.py", line 816, in _parse
p = _parse_sub(source, state, sub_verbose, nested + 1)
File "/usr/lib/python3.7/sre_parse.py", line 426, in _parse_sub
not nested and not items))
File "/usr/lib/python3.7/sre_parse.py", line 651, in _parse
source.tell() - here + len(this))
re.error: nothing to repeat …
Run Code Online (Sandbox Code Playgroud)

python string split

0
推荐指数
1
解决办法
51
查看次数

标签 统计

common-lisp ×1

lisp ×1

python ×1

split ×1

string ×1