Common Lisp lambda expression error

2020-05-08 07:59发布

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条回答
萌系小妹纸
2楼-- · 2020-05-08 08:10

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)))))
查看更多
登录 后发表回答