ProximaC 0 Newbie Poster

I seriously need help with this LISP Programming. There are 8 questions but I've done the first 4 questions. Right now, I'm totally clueless with the rest of the remaining questions.

Please help.

  1. Give iterative definition of a function (symbol-count-iter) that takes a list and a symbol a and returns the number of times the symbol a occurs in it.

(defconstant sym 'a)

(defun symbol-count-iter (lst)

Sample output::

(symbol-count-iter '(a c d e a 2 3))
(symbol-count-iter '(a c d e a 2 3))

  1. Your friend is trying to write a function that returns the sum of all the non-nil elements in a list. He has written two versions of this function, and neither of them works. Explain what’s wrong with each and give a correct version.
    (defun summit (lst)
    (remove nil lst)
    (apply #'+ lst))

(defun summit (lst)
(let ((x (car lst)))
(if (null x)
(summit (cdr lst))
(+ x (summit (cdr lst))))))

  1. Define a function (count-numbers?) which returns a number which counts only numbers in a list.
    (defun count-numbers? (lst)

Sample output:

(count-numbers? '(a b c d 3 4 5 6 nil))

  1. Write a function filter which takes a list and a predicate, and returns the list of the elements from the original list for which the predicate returns true. (There are actually LISP built-ins to do this called remove-if and remove-if-not. You should not use them for this problem.)
    >(defun even(num) (= (mod num 2) 0))
    >(filter '(6 4 3 5 2) #'even)
    (6 4 2)
    (defun filter (lst pred)

Sample output:

(filter '(2 4 5 6 7 8 10 11) #'oddp)
(5 7 11)
(filter '(2 4 5 6 7 8 10 11) #'oddp)
(5 7 11)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.