lundi 2 novembre 2020

Breaking From Loop Through If Statement LISP

I am currently writing a small program in LISP that will take in a list, and split it as follows:

(split '(1 2 3) returns-> ((1 2) (3)) 
(split '(1 2 3 4) returns-> ((1 2) (3 4))

I feel like I am very close and have the basic logic of it written, my issue is that I have to perform 2 - 3 actions if the if statement evaluates to T. Because of this I keep receiving the error

Error(s), warning(s): *** - SYSTEM::%EXPAND-FORM: (PUSH (CAR LST) (CDR (LAST NEWLST))) should be a lambda expression

When the if statement evaluates to true, I need to add the head of the current list to the end of my list variable newLst, then I need to take the tail of the current list and set the value of my other list variable new2Lst to that. After doing this, I need to break out of the loop and append the two lists to each other. This probably does not make much sense, sorry, hopefully the code makes more sense, please find it below. Thank you all in advance for the help, I really appreciate each and every bit of it!

(defun split (lst) 
    (cond ((= (list-length lst) 1) lst) 
          (t   (setq w (list-length lst))
               (setq newLst (list (car lst))) 
               (setq new2Lst '())
               (loop for x from 1 to (+ w 1) do 
                     (if (= x (ceiling (/ w 2))) 
                         ( (push (car lst) (cdr( last newLst))) (setq new2Lst (cdr lst)) (return)) 
                         (push (car lst) (cdr( last newLst)))
                      )  
                     (setq lst (cdr lst))
               ) 
               (append (list newLst) (list new2Lst))
            ) 
     ) 
)

Aucun commentaire:

Enregistrer un commentaire