Printing elements of a list with odd indices in Scheme - scheme

so theres this code called ret-odds
ex: (define (ret-odds lst)
(if (null? lst) null
(if (null? (cdr lst)) null
(cons (car lst) (ret-odds (cdr (cdr lst)))))))
i know the problem is with the last line in that it skips over the second element of the list and gives only the 3rd.....
ex: (ret-odds (list 'a 'g 'e )) the procedure instead skips over the g and e and gives me null so i only end up with a so i was wondering how would i fix this?

Your code is always skipping the last element in the list:
On the first invocation with (list 'a 'd 'e), lst is non-null and (cdr lst) is non-null, so it takes the car ('a) and does a recursive call with the cddr (which is (list 'e))
On the second invocation with (list 'e), lst is non-null but (cdr lst) is null. So it returns null, skipping the 'e completely.
Something like this should work:
ex: (define (ret-odds lst)
(if (null? lst) null
(cons (car lst)
(if (null? (cdr lst)) null (ret-odds (cdr (cdr lst)))))))

Change null of your second if to lst.
(define (ret-odds lst)
(cond ((null? lst) lst)
((null? (cdr lst)) lst)
(else (cons (car lst) (ret-odds (cddr lst))))))

Related

How to fix this code in Scheme, where I am trying to find the last two elements in the list

I am trying to write a code in scheme -last_two(lst) where it should output the last two elements in the list.
(define (last_two lst)
(if (null? lst)
'() )
(if (null? (cdr lst))
lst)
(if(null? (cddr lst)))
lst)
(last_two (cdr lst)))
last_two(1 2 3 4) -> (3 4)
A procedure returns the value of the last expression that's executed in the body. In your procedure, when one of the first if conditions is true, you don't return the value from the procedure, you just go on to test the next condition. Eventually you get to the end, which is the recursive call -- you never stop at the base case. Eventually you'll get an error when you try to call (cdr lst) on an empty list.
You can use cond to test a series of conditions and return the value from the first one that's true.
Also, you should be returning lst when the conditions match; your code will always return (), since you're returning the same thing that matched the null? condition.
(define (last_two lst)
(cond ((null? lst) '())
((null? (cdr lst)) lst)
((null? (cddr lst)) lst)
(else (last_two (cdr lst)))))
(last_two '(1 2 3 4))
You might notice that the first 3 conditions all return lst, so you could combine them using or.
(define (last_two lst)
(if (or (null? lst) (null? (cdr lst)) (null? (cddr lst)))
lst
(last_two (cdr lst))))

Is there anything wrong with my "sum of list" code in scheme?

My else statement line is giving me an error. Is any of my other line of codes affecting the else expression?
(define (sumAdd list)
(cond
((null? list) '())
((null? (cdr list)) list)
((symbol? list) sumAdd(cdr list))
(else (+ (car list)(sumAdd (cdr list))))
)
)
If I understand correctly, you want to sum all the numbers in a list with mixed element types. If that's the case, there are several errors in your code:
(define (sumAdd list) ; `list` clashes with built-in procedure
(cond
((null? list) '()) ; base case must be zero for addition
((null? (cdr list)) list) ; why discard the last element?
((symbol? list) sumAdd(cdr list)) ; that's not how procedures are called
(else (+ (car list) (sumAdd (cdr list)))))) ; this line is fine :)
This is the correct way to implement the procedure:
(define (sumAdd lst)
(cond
((null? lst) 0) ; base case is zero
((symbol? (car lst)) (sumAdd (cdr lst))) ; skip current element
(else (+ (car lst) (sumAdd (cdr lst)))))) ; add current element
It works as expected:
(sumAdd '(1 a 2 b 3 c))
=> 6

How to reverse nested lists in Scheme

Consider:
(define (nested-reverse lst)
(cond ((null? lst) '())
((list? (car lst)) (nested-reverse (car lst)))
(else
(cons (nested-reverse (cdr lst))
(list (car lst))))))
When I input,
(nested-reverse '((a b c) 42))
it gives me ((() 42) (a b c)). It's supposed to give me (42 (c b a)). How I would change my code so that the nested lists also get reversed?
Keep in mind that a list (1 2 3) is (cons 1 (cons 2 (cons 3 '()))). Using append is a very poor choice on how to reverse a list since append is implemented like this:
(define (append lst1 lst2)
(if (null? lst1)
lst2
(cons (car lst1) (append (cdr lst1) lst2))))
A list can be iterated from the first element towards the end while it can only be made in reverse. Thus the obvious none recursive reverse would look like something like this:
(define (simple-reverse lst)
(let loop ((lst lst) (result '()))
(if (null? lst)
result
(loop (cdr lst) (cons (car lst) result)))))
To make it work for nested list you check if you need to reverse (car lst) by checking of it's a list or not and use the same procedure as you are creating to do the reverse on the element as well. Other than that it's very similar.

Racket Append Undesired Reversal of List

I wrote a small function that takes a list and returns a list composed of only positive numbers. This all works fine, but for some reason, it is reversing the order. More information below on that. Could someone please tell me if this is normal, or if I miss-wrote something? Thank you in advance.
(define (positive-nums-only lst)
(if (empty? lst)
'()
(append (positive-nums-only (cdr lst))
(if (>= (car lst) 0)
(list (car lst))
'()))))
(positive-nums-only '(1 2 -4 90 -4))
The above test case returns '(90 2 1)
You did not make a mistake, the program is making what you asked.
See, the program finishes the recursion calls first, before going into resolving the if statement. This causes the (list ... ) to start listing from the last element that is positive, in this example it will be 90.
Changing the code order will produce the result you want.
(define (positive-nums-only lst)
(if (empty? lst) '()
(append (if (>= (car lst) 0 )
(list (car lst))
'())
(positive-nums-only (cdr lst)))
)
)
On the other hand, this kind of recursion could be expensive to the computer. I'd use tail recursion, like this:
(define positive-nums-only-tail
(λ (lst r)
(cond ((empty? lst) (reverse r))
((positive? (car lst))
(positive-nums-only-tail (cdr lst)
(cons (car lst) r)))
(else (positive-nums-only-tail (cdr lst) r))
)
)
)
Have you tried reversing the append?
(define (positive-nums-only lst)
(if (empty? lst)
'()
(append (if (>= (car lst) 0) (list (car lst)) '())
(positive-nums-only (cdr lst)))))
Personally I find it more natural to write it like this:
(define (positive-nums-only lst)
(if (empty? lst)
'()
(let ((rest (positive-nums-only (cdr lst))))
(if (>= (car lst) 0)
(cons (car lst) rest)
rest))))

How to define Sets in scheme

I'm having trouble using my member? function. I need to recurse on my set? function until the last element in my list 'lst' is reached. I believe I have the navigation down correctly, but maybe my inputs syntax is wrong. I know there are three cases:
1) What happens if the list is empty? that means that there aren't any duplicates in it
2) What happens if the current element of the list exists somewhere in the rest of the list? then it means that there's a duplicate in the list (hint: the member procedure might be useful)
3) If none of the above are true, continue with the next element.
Here is my code.
(define (member? e lst)
(if (null? lst) #f
(if (equal? e (car lst)) #t
(member? e (cdr lst)))))
(define (set? lst)
(if (null? lst) #t ;Case1
(if (member? (car lst) lst) #f ;Case2
(set? (cdr lst))))) ;Case3
;Example tests for the set? function
(set? '(x y z))
(set? '(a 1 b 2 c 3))
(set? '())
(set? '(6 2 2))
(set? '(x y z x))
There's a small mistake with your code, look how it gets fixed:
(define (set? lst)
(if (null? lst)
#t
(if (member? (car lst) (cdr lst)) ; in here
#f
(set? (cdr lst)))))
In particular, notice what this line is doing:
(member? (car lst) lst)
That won't work: the test is checking whether the first element in lst is a member of lst - and that'll always be true. The solution is simple, just check to see if the current element is in the rest of the list, if it's there, then we know that we've found a duplicate:
(member? (car lst) (cdr lst))
And by the way, the above code would look much nicer using cond, which is great when you have nested ifs:
(define (set? lst)
(cond ((null? lst) #t)
((member? (car lst) (cdr lst)) #f)
(else (set? (cdr lst)))))

Resources