Prevent double invocation of recursive function in Common Lisp - recursion

I have that Lisp function:
(defun F(l)
(cond
((atom l) -1)
((>(F(car l))0)(+(car l)(F(car l))(F(cdr l))))
(t(F(cdr l)))
)
)
and I want to prevent double invocation of recursive function (F (car l)) in the second line of cond using a lambda function.
I tried that:
(defun F(l)
((LAMBDA (ff)
(cond
((atom l) -1)
((> ff 0)(+(car l) ff (F(cdr l))))
(t(F(cdr l)))
)
) (F (car l)))
)
but I get error :
CAR: 1 is not a list
at call (F '(1 2 3 4)).
Also I'm not sure if that correctly avoids double recursive call.

The reason you are getting this error is that you are calling (car l) always, even when l is, in fact, not a list.
To avoid this, only do that when needed:
(defun F(l)
(if (atom l) -1
(let* ((l1 (car l))
(f1 (F l1))
(f2 (F (cdr l))))
(if (plusp f1)
(+ l1 f1 f2)
f2))))

Related

Sort an element and put it in a variable

I have fowling task:
Write a recursive function SORT-LIST which, from a list of any number of "apples" and "peas", sorts out the "apples" and stores them in an optional variable and at the end returns the contents of this optional variable.
I have no idea how to fix ist. That's my beginning. Maybe there is someone who could help me. Thanks a lot!!
(defun sort-list (x l)
(cond ((null l) nil))
((equal (first l) x)
(cons (first l) (sort-list x (rest l))))
((sort-list x (rest l))))
The name is misleading. It is not sort but actually a filter.
(defun applep (x)
"something looking whether x is an apple or not")
(defun my-filter (pred l &optionals (acc '()))
(cond ((null l) (nreverse acc))
((funcall pred (car l)) (my-filter pred (cdr l) (cons (car l) acc)))
(t (my-filter pred (cdr l) acc))))
Which is in-built in lisps - so even without defining it, you could run:
(filter #'applep l)

fix the function which removes elments [Common Lisp]

The task is: for given list of elements and X element, remove an element after X if it is not equal to X. Example: (a 8 2 a a 5 a) X=a, expecting (a 2 a a a).
I have code that removes an element before X, so it gives me (a 8 a a a) instead. How do I fix it?
(defun purgatory (n w)
(cond ((null w) nil)
((and (eq (cadr w) n) (not (eq (car w) (cadr w)))) (purgatory n (cdr w)))
((cons (car w) (purgatory n (cdr w))))))
You can use the destructuring of for on clauses in loop:
(defun purgatory (list x)
(cons (first list)
(loop :for (a b) :on list
:unless (and (eql a x)
(not (eql b x)))
:collect b)))
I think you are on the right lines with a recursive algorithm. I think that the algorithm works better as a tail-optimised recursion. You take an in-list and an X, and build up an out-list. The output is reversed, and so reverse needs to be applied at the end, thus:
(defparameter my-list '(a 8 2 a a 5 a))
(defun remove-after (in-list X &optional (out-list '()) (last '()))
(if (null in-list)
(reverse out-list)
(if (and (eql last X) (not (eql (car in-list) X)))
(remove-after (cdr in-list) X out-list (car in-list))
(remove-after (cdr in-list) X (cons (car in-list) out-list) (car in-list))
)))
; (A 2 A A A)
As for the non-tail algorithm, I think this does it:
(defun purgatory (n w)
(cond ((null w) nil)
((and (eq (car w) n) (not (eq n (cadr w)))) (cons (car w) (purgatory n (cddr w))))
(t (cons (car w) (purgatory n (cdr w))))
))
; (A 2 A A A)
So, if the first element is n and the next is not n, then add n at the front of the algorithm, but skip cddr the next element. Otherwise, add the first element to the front of the algorithm, no skip cdr.
NB: since you've defined the problem in terms of X, I think this should be one of your parameters, not n

Unbound Variable in lisp function, I have no clue why

So im halfway losing my mind over this lisp code im working on. Im not allowed to use most lisp functions so I have to focus on basic commands and recursion. The function is supposed to find the list with the passed matching variable. checkInner works on all my tests, but for some reason asso is throwing an exception "UNBOUND-VARIABLE", if someone could analyze this and explain I would greatly appreciate it. THANKS!
(defun checkInner(A L)
(cond ((and (atom L) (eq A L)) t)
(t (cond ((or (atom L) (eq A L)) nil)
(t (cond ((eq A (car L)) t)
(t (checkInner A (cdr L))
)
)
)
)
)
)
)
(defun my-assoc(A L)
(cond ((checkInner A (car L)) (car L))
((eq (cdr L) nil) nil)
(t (my-assoc A (cdr L)))
)
)
;test cases
(my-assoc 'a nil) ;works
(my-assoc 'a '((a . b)(c e f)(b))) ;works
(my-assoc 'c '((a . b)(c e f)(b))) ;does not work

Why is this function returning true when it should be returning false?

So, I have this helper function that checks to see if there is a reflexive relationship between a list and a list of pairs.
(define helper
(lambda (L S)
(cond
((if (equal? L '()) #f ;; here, when L equals empty list, it should return #f, but somehow it returns #t even if L is '().
(if (equal? S (car (car L)))
(if (list-equal? (car L))#t
(helper (cdr L) S))
(helper (cdr L) S))))
)))
However, the part where it checks if L is an empty list returns true even if the list is an empty list, allowing my other function to return true.
I've been stumped trying to figure out why its returning #t instead of #f for hours. Please help me figure out what's making this happen.
Oh and I'm using Dr.Racket version 6.12.
EDIT: more clearly, I would like the function to return #f when L is '() as a base case so that the function doesn't need to do anymore recursion.
You put if forms within cond which is quite superfluous.
So your mistake was for sure your lack of understanding of the cond syntax.
Remember cond syntax goes like:
(cond (condition1 what-to-do-if-condition1-is-true)
(condition2 what-to-do-if-condition2-is-true)
( ... ... )
(else what-to-do-if-none-of-the-conditions-listed-above-evaluated-to-true))
So I formed your expression accordingly to:
(define helper
(lambda (L S)
(cond ((equal? L '()) #f)
((and (equal? S (car (car L))) (list-equal? (car L))) #t)
(else (helper (cdr L) S)))))
Since you didn't gave definition for list-equal? - I cannot run this code for testing.
You have nested if in cond. Lets rewrite you code som something identical:
(define helper
(lambda (L S)
(let ((result
(if (equal? L '())
#f
(if (equal? S (car (car L)))
(if (list-equal? (car L))
#t
(helper (cdr L) S))
(helper (cdr L) S)))))
(cond
(result result)
(else 'implementation-defined-value)))))
A cond will return a implementation defined value as the else clause should none of the previous predicates hit. Since your base casse returns #f it goes to the default else case.
Since the other answer show the code with cond, here is the same with if:
(define helper
(lambda (L S)
(if (equal? L '())
#f
(if (and (equal? S (car (car L)))
(list-equal? (car L)))
#t
(helper (cdr L) S)))))
You can also write this only with and and or:
(define helper
(lambda (L S)
(and (pair? L)
(or (and (equal? S (car (car L)))
(list-equal? (car L)))
(helper (cdr L) S)))))

Scheme - Recursive cases

I'm finishing up a Scheme assignment and I'm having some trouble with the recursive cases for two functions.
The first function is a running-sums function which takes in a list and returns a list of the running sums i.e (summer '(1 2 3)) ---> (1 3 6) Now I believe I'm very close but can't quite figure out how to fix my case. Currently I have
(define (summer L)
(cond ((null? L) '())
((null? (cdr L)) '())
(else (cons (car L) (+ (car L) (cadr L))))))
I know I need to recursively call summer, but I'm confused on how to put the recursive call in there.
Secondly, I'm writing a function which counts the occurrences of an element in a list. This function works fine through using a helper function but it creates duplicate pairs.
(define (counts L)
(cond ((null? L) '())
(else (cons (cons (car L) (countEle L (car L))) (counts (cdr L))))))
(define (countEle L x)
(if (null? L) 0
(if (eq? x (car L)) (+ 1 (countEle (cdr L) x)) (countEle (cdr L) x))))
The expected output is:
(counts '(a b c c b b)) --> '((a 1) (b 3) ( c 2))
But it's currently returning '((a . 1) (b . 3) (c . 2) (c . 1) (b . 2) (b . 1)). So it's close; I'm just not sure how to handle checking if I've already counted the element.
Any help is appreciated, thank you!
To have a running sum, you need in some way to keep track of the last sum. So some procedure should have two arguments: the rest of the list to sum (which may be the whole list) and the sum so far.
(define (running-sum L)
(define (rs l s)
...)
(rs L 0))
For the second procedure you want to do something like
(define (count-elems L)
(define (remove-elem e L) ...)
(define (count-single e L) ...)
(if (null? L)
'()
(let ((this-element (car L)))
(cons (list this-element (count-single this-element L))
(count-elems (remove-elem this-element (cdr L)))))))
Be sure to remove the elements you've counted before continuing! I think you can fill in the rest.
To your first problem:
The mistake in your procedure is, that there is no recursive call of "summer". Have a look at the last line.
(else (cons (car L) (+ (car L) (cadr L))))))
Here is the complete solution:
(define (summer LL)
(define (loop sum LL)
(if (null? LL)
'()
(cons (+ sum (car LL)) (loop (+ sum (car ll)) (cdr LL)))))
(loop 0 LL))

Resources