function - LISP, cond returning nil everytime -
why return nil when adding lst???
please help!
thank you!
cl-user 1 : 1 > (defun constants-aux (form lst) (cond ((null form) lst) ((eq (car form) 'implies) (constants-aux (cdr form) lst)) ((eq (car form) 'not) (constants-aux (cdr form) lst)) ((eq (car form) 'and) (constants-aux (cdr form) lst)) ((eq (car form) 'or) (constants-aux (cdr form) lst)) ((atom (car form)) (print (car form)) (cons (car form) (list lst)) (delete-duplicates lst) (constants-aux (cdr form) lst)) (t (constants-aux (car form) lst) (constants-aux (cdr form) lst)))) constants-aux cl-user 2 : 1 > (defun constants (form) (constants-aux form nil)) constants cl-user 3 : 1 > constants '(implies (not q) (implies q p)) q q p nil
you doing wrong in many ways.
1. - why create -aux function, when can use optional arguments?
(defun constants (form &optional lst) (cond ((null form) lst) ...
2. - don't need many similar branches, can write:
((find (car form) '(implies not , or)) (constants (cdr form) lst))
3. - delete-duplicates may modify list, don't think have to, , if will, modification makes not wanted. should use result of it. think got style-warning, in sbcl looks code:
; caught style-warning: ; return value of delete-duplicates should not discarded.
read warnings, helps.
i don't understand result expecting, can't modify function work properly, i'll try show problem(s) is. code last 2 cond branches:
((atom (car form)) (print (car form)) (cons (car form) (list lst)) ;; result ignored (delete-duplicates lst) ;; result ignored (constants-aux (cdr form) lst))
you should write:
(constant-aux (cdr form) (delete-duplicates lst)) (t (constants-aux (car form) lst) ;; result ignored (constants-aux (cdr form) lst))))
may (depends on want get) should write:
(cons (constants-aux (car form) lst) (constants-aux (cdr form) lst))
4 i'm not shure, looks using print debug, use trace instead. code it'll give pretty information going on list during execution:
0: (constants-aux (implies (not q) (implies q p)) nil) 1: (constants-aux ((not q) (implies q p)) nil) 2: (constants-aux (not q) nil) 3: (constants-aux (q) nil) q 4: (constants-aux nil nil) 4: constants-aux returned nil 3: constants-aux returned nil 2: constants-aux returned nil 2: (constants-aux ((implies q p)) nil) 3: (constants-aux (implies q p) nil) 4: (constants-aux (q p) nil) q 5: (constants-aux (p) nil) p 6: (constants-aux nil nil) 6: constants-aux returned nil 5: constants-aux returned nil 4: constants-aux returned nil 3: constants-aux returned nil 3: (constants-aux nil nil) 3: constants-aux returned nil 2: constants-aux returned nil 1: constants-aux returned nil 0: constants-aux returned nil
5. it'll easyer answer if explain transformation expecting code.
good luck.
Comments
Post a Comment