0
votes

i am trying to write a scheme program which will take a list of marks as input and gives the output as a list of the grades.

i got this far , .. i dunno whats wrong i get an error the object () passed as the first argument to cdr is not the correct type ....

here is the code

(define (grades list1)
  (cons (cond ((= (car list1) 100) 'S)
              ((= (car list1) 90) 'A)) 
        (cons (grades (cdr list1)) '())))
2
Only 3 lines of code, and already the parens are giving me a headache ^^Thomas Levesque
@Thomas: Only because the formatting is crap. I'm going to fix it right now. (No matter what language you code in, if someone uses non-standard formatting, it will cause headaches for everyone else who reads it.)Chris Jester-Young
@Ross: Nice, I see your suggested edit. Standard indentation in Lisp and Scheme code is two spaces, BTW. (All the other places where I had more than two spaces are, of course, for alignment purposes.)Chris Jester-Young
@Chris, "Standard indentation in Lisp and Scheme code is two spaces" -- chapter and verse, please? Or did you mean somthing like "my preferred indentation" or "the most usual indentation"?hmakholm left over Monica
@Henning: It's obviously not a standard, of course -- that would mean a language that is sensitive to indentation. It's "standard" in the sense of being extremely common. In the same way that putting a newline before a ; in C is non-standard. (And since neither of these practices is anywhere close to being in an language standard proper, debating this point more is wasting breath for no good reason.)Eli Barzilay

2 Answers

5
votes

You're missing a base case for your recursion. How do you want your grades function to behave when the argument is an empty list? This requires an outer cond that tests is the list is empty and returns something appropriate when it is.

0
votes
(define (grades list1)
  (cond((null? list1) `())
       (else(cons (cond ((= (car list1) 100) 'S)
                        ((= (car list1) 90) 'A)) 
                  (grades (cdr list1))))))