Common Lisp lambda expression error

社会主义新天地 提交于 2019-12-02 18:13:18

问题


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

And here is my code

(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)

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.

I am getting this error:

*** - SYSTEM::%EXPAND-FORM:
(COND ((NULL INPUT) NILL)(T(OR (COND ((ATOM (CAR INPUT)) (COND ((NOT (SEARCHDEEP (CAR INPUT) OUTPUT))(APPEND OUTPUT INPUT)))) (T (REMDOUBLES (CAR INPUT) OUTPUT))) (REMDOUBLES (CRD INPUT) OUTPUT)))) should be a lambda expression

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


回答1:


Your code:

First line:

(DEFUN REMDOUBLES (INPUT OUTPUT)

Second line: Can you explain what the single parentheses should do?

(

You remember the Lisp syntax for basic Lisp expressions?

(operator argument0 argument1 ... argumentn)

It's not

((operator argument0 argument1 ... argumentn))

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

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

Then learn there how Lisp code looks like.

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))
        )
    )
)
))

This is the formatted code:

(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))))))

But it would be even better written as:

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


来源:https://stackoverflow.com/questions/39811497/common-lisp-lambda-expression-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!