It's up to you...
But if you want to learn about recursion the answer is 'yes, you need to!'
Check if a list contains only numbers (assume it's not empty).
CL-USER> (defun numbersp (lst)
(cond
((numberp (car lst)) 'car-is-a-number)
(nil)))
NUMBERSP
CL-USER> (numbersp '(1 2 3))
CAR-IS-A-NUMBER
CL-USER> (numbersp '(a 2 3))
NIL CL-USER> (defun numb (lst)
(cond
((numberp (car lst)) (numb (cdr lst)))
(nil)))
(defun numbersp (lst)
(cond
((null lst) T)
((numberp (car lst)) (numbersp (cdr lst))))) (defun numbersp (lst)
(cond
((null lst) T)
((numberp (car lst)) (numbersp (cdr lst)))
(t nil)))
Philipp wrote:For simlicity, the task was defined to work with not-empty lists only. So very good!
By the way, though cond returns nil by default, I think it's convention to always state the 'fall-through'-case in cond, like so:
- Code: Select all
(defun numbersp (lst)
(cond
((null lst) T)
((numberp (car lst)) (numbersp (cdr lst)))
(t nil)))
This way it's clear that you didn't just forgot a case.
Philipp wrote:Extract the numbers!
- Code: Select all
(defun numbers (lst) ...)
> (numbers '(1 2 a b 3 c 4 1)) => (1 2 3 4 1)
(defun numbers (lst)
(labels ((is-number (elt)
(numberp elt)))
(delete-if-not #'is-number lst))) (defun numbers (lst)
(setq new-lst '())
(cond
((numberp (car lst)) (cons (car lst) new-lst) (numbers (cdr lst)))
((null lst) new-lst)
(t (numbers (cdr lst))))) (let ((new-lst (list)))
(defun numbers (lst) ...)
(defun numbers (lst)
(let ((new-lst (list)))
(labels ((list-walker (list) ....))
(list-walker lst)
(nreverse new-lst)) ;assuming you're using push or cons to build your list you need to reverse it when you're done(defvar *a* 3) ;*a* is now defined as special
(defun test-a () *a*)
(test-a) ;This yields 3
(let ((*a* 5)) ;Even though we use let, *a* was already special and will not become lexical
(test-a) ;Because *a* is special, we can shadow it at run-time with let, and the effect will propagate to functions defined earlier
(let ((b 4)) ;b is now lexical
(defun test-b () b)) ;Since b is lexical, it's value will be locked in when we compile this function
(test-b) ;this yields 4
(let ((b 6))
(test-b) ;this also yields 4, because due to b's lexical nature, we cannot shadow it any more.sycamorex wrote:
- Code: Select all
(defun numbers (lst)
(labels ((is-number (elt)
(numberp elt)))
(delete-if-not #'is-number lst)))
Users browsing this forum: No registered users and 3 guests