0

My goal is to find from this list

(A B C D E G)

a new list

(B D G)

But my code is not working.

(define (fun lst) (cond ((null? lst) '()) ((null? (cdr lst) '()) (else (cons ( cadr lst) ( fun lst)))) 

I'm getting (B C D E G). Where have I gone wrong?

3
  • 3
    possible duplicate of A scheme procedure that returns a list of every other element Commented Nov 11, 2012 at 5:49
  • Another one? it seems that several students from this particular programming course have come to dump their homework in Stack Overflow. This question has been answered already, here. Commented Nov 11, 2012 at 5:51
  • Look at that else branch, do you think your recursion is right there? Commented Nov 11, 2012 at 5:53

1 Answer 1

2

Your function isn't doing anything and you really haven't specified what you want to test. If you really only want to get B D G from that specific list then all you need to do is make those each tests, (equal? (car lst) 'B) and so on for each character.

If instead the function is suppose to just print every other character in a list then you need to construct a way to do that. For example your base case for recursion that you have now is correct, and empty list should return an empty list. Otherwise if it is not empty return the the cdr of the list and then work with that.

If you still can't figure out an answer just start writting it down on paper and see what different tests will do. You need to come up with a way to find every other character.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.