开发者

What is wrong with scheme code?

开发者 https://www.devze.com 2023-03-29 10:55 出处:网络
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 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)) '())))


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号