recursion - Why does this lisp function return nil? -
i trying build lisp function car , cdr through list while comparing values in list max value. if value in list larger max value value replaced max value. wondering why getting return value of nil? new lisp , come java background quite different me. not allowed use object oriented stuff , limited car, cdr, append, cons. want return list "elements2".
(defun enforce-upper-limit(maxval elements elements2) (when elements (cond ((greaterthanmax (car elements) maxval)(enforce-upper-limit maxval (cdr lst) (append (maxval)))) (t (enforce-upper-limit maxval (cdr elements)(append (car elements))))) ) )
formatting & naming
common lisp case insensitive maxval , greaterthanmax become unreadable. separate parts of name - , make lower-case.
spaces: open parenthesis should have space in front of it.
indentation: 1 has various rules, long story short: emacs in lisp mode right thing on <tab>
finally, don't make lines long, usual rule of 80 characters universal.
after tidying follows:
(defun enforce-upper-limit (max-val elements elements2) (when elements (cond ((greater-than-max (car elements) max-val) (enforce-upper-limit max-val (cdr lst) (append (max-val)))) (t (enforce-upper-limit max-val (cdr elements) (append (car elements)))))) problems in code
some obvious problems:
line 4: expression
(max-val)call function calledmax-valno arguments. doubt want.function
appendappends lists. in both cases of use neither given list append nor has list append to.function
enforce-upper-limit@ best returnnil.what
lst? shouldelements.
let's start fixing. first, let's deal problem 3. when elements nil, answer should accumulated in elements2. also, whole conditional expression resembles if... elseif ... pattern. so, let's try arrange in 1 cond:
(defun enforce-upper-limit (max-val elements elements2) (cond ((null elements) elements2) ((greater-than-max (car elements) max-val) (enforce-upper-limit max-val (cdr lst) (append (max-val)))) (t (enforce-upper-limit max-val (cdr elements) (append (car elements))))) next, let's see want achieve. second clause states if (car elements) large, max-val should go elements2. , if (car elements) ok (the last clause), added elements2.
adding 1 item list job cons, not append. in both cases consing elements2 expression should (cons elements2). also, replacement of lst elements produces (almost) correct program:
(defun enforce-upper-limit (max-val elements elements2) (cond ((null elements) elements2) ((greater-than-max (car elements) max-val) (enforce-upper-limit max-val (cdr elements) (cons max-val elements2))) (t (enforce-upper-limit max-val (cdr elements) (cons (car elements) elements2))))) further issues
not sure if there need in
greater-than-maxsimple>suffice.items in
elements2in wrong order comparedelements(i leave figure out). usual problem of iterative algorithms on lists.
finally, more cl way of doing be
(defun enforce-upper-limit (list max-val) (loop x in list if x > max-val collect max-val else collect x)) but might not acceptable assignment.
Comments
Post a Comment