2

I am trying to write a GPA Calculator in Scheme language. The only part I think I am having problem with is the part in which I need to convert letters to integers within a list. I am using eq? in the code below because in one of his examples, our professor used eq? to convert a letter to an integer.

(define creditSum 
  (lambda (lst)
    (if (or (not (list? lst)) (empty? lst))
        0
        (+ (car lst) (creditSum (cdr lst))))))

(define (gpa lst x)
  (cond
   ((eq? x "A") 4.0)
   ((eq? x "B") 3.0)
   ((eq? x "C") 2.0)
   ((eq? x "D") 1.0)
   ((eq? x "F") 0.0)
   (/ (creditSum x) (length x))))

I want to assign a number to each letter, and use those letters to get my output as an integer or decimal. For example if I input: (gpa '(A B A B)) my output should be 3.5.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
Zzhh82
  • 85
  • 1
  • 7
  • "eq?" might not be the right comparison to use for strings. `(eq? "a" "a")` isn't necessarily true. Instead, you need to use [equal?](http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html#%_idx_218). – Joshua Taylor Jun 30 '15 at 21:46
  • 1
    possible duplicate of [What is the difference between eq?, eqv?, equal?, and = in Scheme?](http://stackoverflow.com/questions/16299246/what-is-the-difference-between-eq-eqv-equal-and-in-scheme) – Joshua Taylor Jun 30 '15 at 21:46
  • eq? does not convert a letter to an integer. eq? is an equivalence relationship which you can read more about in [the documentation](http://www.schemers.org/Documents/Standards/R5RS/HTML/). Your professor may have made a mistake, or you may have misunderstood your professor, but eq? does not convert letters to integers. – Joshua Taylor Jun 30 '15 at 21:51
  • In `(eq? x "D")`, you're comparing a string and the value of a variable `x`. When you write `'(A B A B)`, you have a list of symbols (*not strings!*), so the value of `x` is going to be a symbol. No symbol is going to be equal to a string. – Joshua Taylor Jun 30 '15 at 21:54
  • So is there any build-in function that I could use to convert a symbol to integer? – Zzhh82 Jun 30 '15 at 22:01
  • What symbol would you be trying to convert to an integer? – Joshua Taylor Jun 30 '15 at 22:02
  • What it seems that you really *want* to be doing here is *mapping* a grade (whether it's represented as a string, a symbol, etc., is a different question) to a number, and then taking the average of a list of numbers. – Joshua Taylor Jun 30 '15 at 22:04
  • Sounds good, I will try mapping to see if I could get it to work. Thanks! – Zzhh82 Jun 30 '15 at 22:06

1 Answers1

2

You're confounding concepts. First, you have to transform a grade to a number, for this you can use a helper procedure, and notice that it's better to use symbols instead of strings (faster comparison, easier to write):

(define (grade->number x)
  (cond
    ((eq? x 'A) 4.0)
    ((eq? x 'B) 3.0)
    ((eq? x 'C) 2.0)
    ((eq? x 'D) 1.0)
    ((eq? x 'F) 0.0)))

Now we just have to apply the above procedure to each of the elements, add them and find the average - this is the part where a higher-order function comes into play, see how we use map:

(define (gpa lst)
  (/ (creditSum (map grade->number lst))
     (length lst)))

In fact, credit-sum can also be expressed using existing procedures - there's no need to reinvent the wheel!

(define (creditSum lst)
  (apply + lst))

For example:

(gpa '(A B A B))
=> 3.5
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Nice answer. It would be even nicer to use symbols instead of strings, so you could do `(gpa '(A B C))` instead of `(gpa '("A" "B" "C"))`. With the advantage of being able to use `eq?` for comparison (it's faster and easier to type!) – Jay Jul 01 '15 at 13:21
  • 1
    @Jay That's a good suggestion, I tried to follow OP's attempted solution but it's better to show him/her the preferred way of doing things in Scheme. I edited my answer :) – Óscar López Jul 01 '15 at 15:24
  • 1
    Thank you so much!!! I have been struggling with this question for many hours. Now I understand how it works! It really make sense now. I had stuck on the part of converting letters to numbers. And also I like how you make a short and nice procedure for creditSum! When I compare it to mine....! lol – Zzhh82 Jul 02 '15 at 00:49