常见的Lisp Lambda表达式错误 [英] Common Lisp lambda expression error

查看:131
本文介绍了常见的Lisp Lambda表达式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个程序,该程序将重写给定的行,而输出行中没有重复项。我使用此网站作为编译器:
https://www.tutorialspoint.com/execute_lisp_online。 php

I am trying to make a program, which will rewrite given row without duplicates in an output row. I am using this website as a compiler: https://www.tutorialspoint.com/execute_lisp_online.php

这是我的代码

(SETQ X (LIST  2 -3 (LIST 4 3 0 2) (LIST 4 -4) (LIST 2 (LIST 2 0 2))-3))'
(DEFUN SEARCHDEEP (WHAT WHERE) ;Function will find out if atom `WHAT`is in a row `WHERE` => works fine
(COND
    ((NULL WHERE) NIL)
    (T (OR 
            (COND 
                ((ATOM (CAR WHERE)) (EQUAL WHAT (CAR WHERE)))
                (T (SEARCHDEEP WHAT  (CAR WHERE)))
            )
            (SEARCHDEEP WHAT (CDR WHERE))
        )
    )
)
)

(DEFUN REMDOUBLES (INPUT OUTPUT)
(
(COND 
    ((NULL INPUT) NILL) ;recursion exit
    (T                  ; funstion
        (OR             ; step into or go forward
            (COND 
                ((ATOM (CAR INPUT)) (COND 
                                        ((NOT (SEARCHDEEP (CAR INPUT) OUTPUT)) (APPEND OUTPUT INPUT)) ;if this atom wasn't added => append
                                    )
                )
                (T (REMDOUBLES (CAR INPUT) OUTPUT)) ; step into (car input => list
            )
            (REMDOUBLES (CRD INPUT) OUTPUT) ; go forward, car input is anatom
        )
    )
)
)
)

(SETQ OUT (QUOTE)) ;Empty row
(REMDOUBLES X OUT)
(PRINT OUT)

我上次用过2小时在堆栈上检查此代码和其他答案,但是我不知道这里缺少什么。

I have spent last 2 hours checking this code and other answers here, on stack, however I have no clue what am I missing here.

我收到此错误:

***-SYSTEM ::%扩展格式:

(COND((NULL INPUT)NILL))(T(OR(COND [[ATOM(CAR INPUT))(COND([NOT(SEARCHDEEP(CAR INPUT)OUTPUT)] [APPEND OUTPUT INPUT))))([T(REMOUUBLES(CAR INPUT)OUTPUT))))(REMOUBLES(CRD INPUT)OUTPUT) )))应该是lambda表达式

很抱歉,我是函数式编程和Lisp的新手,我也不知道该怎么做

Sorry for the formatting, I am novice in Functional Programming and Lisp, and I have no idea how it should be done properly.

推荐答案

您的代码:

第一行:

(DEFUN REMDOUBLES (INPUT OUTPUT)

第二行:您能解释单括号应该做什么吗?

(

您还记得基本Lisp表达式的Lisp语法吗?

You remember the Lisp syntax for basic Lisp expressions?

(operator argument0 argument1 ... argumentn)

不是

((operator argument0 argument1 ... argumentn))




很抱歉,我是函数式编程和LISP的新手,我也不知道应该如何正确完成操作。

Sorry for the formatting, i am novice in functional programming and LISP, and i have no idea how it should be done properly.

从Touretzky下载Lisp入门书籍: https://www.cs.cmu.edu/~dst/LispBook/

Download the introductory Lisp book from Touretzky: https://www.cs.cmu.edu/~dst/LispBook/

然后在那里学习Lisp代码的样子。

Then learn there how Lisp code looks like.

您还可以使用Lisp为您格式化代码:

You can also use Lisp to format the code for you:

这是您未格式化的代码:

This is your unformatted code:

[4]> (pprint '(DEFUN SEARCHDEEP (WHAT WHERE) ;Function will find out if atom `WHAT`is in a row `WHERE` => works fine
(COND
    ((NULL WHERE) NIL)
    (T (OR 
            (COND 
                ((ATOM (CAR WHERE)) (EQUAL WHAT (CAR WHERE)))
                (T (SEARCHDEEP WHAT  (CAR WHERE)))
            )
            (SEARCHDEEP WHAT (CDR WHERE))
        )
    )
)
))

这是格式化代码:

(DEFUN SEARCHDEEP (WHAT WHERE)
 (COND ((NULL WHERE) NIL)
  (T
   (OR
    (COND ((ATOM (CAR WHERE)) (EQUAL WHAT (CAR WHERE)))
     (T (SEARCHDEEP WHAT (CAR WHERE))))
    (SEARCHDEEP WHAT (CDR WHERE))))))

但是最好写成这样:

(DEFUN SEARCHDEEP (WHAT WHERE)
  (WHEN WHERE
    (OR (IF (ATOM (CAR WHERE))
            (EQUAL WHAT (CAR WHERE))
          (SEARCHDEEP WHAT (CAR WHERE)))
        (SEARCHDEEP WHAT (CDR WHERE)))))

这篇关于常见的Lisp Lambda表达式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆