Search code examples
schemelispracketmit-scheme

#!unspecific error while printing values in Scheme


I keep getting my output as :

Exam Avg: 50.#!unspecific

Whenever I try to print my program in scheme. I am using two functions print and secprint where I think the error might be happening:

(define (print cnt List)
    (if (= cnt 1) (printEmp (car List))
        (secprint cnt List )))

(define (secprint cnt List)
    (printEmp (car List))
    (newline)
    (print (- cnt 1) (cdr List)))

Could someone please help with this? I am new to scheme and can't seem to figure out where I am going wrong.

Edit: Other code that might be useful.


(define (avg List)
  (cond ((string=? "std" (car (split List)))
  (display (/ (examTotals List) 3.0)))
  (else 0))
)




Solution

  • The display procedure writes to output, and then returns an unspecified value. You use display for its side-effect of printing data, not for its return value.

    (define (avg List)
      (cond ((string=? "std" (car (split List)))
             (display (/ (examTotals List) 3.0)))
            (else 0)))
    

    The avg procedure returns the value of the last expression evaluated; when the display branch evaluates, the unspecified value is returned to the caller. But the caller, printEmp does not need avg to display its results, it only needs a returned value which is then printed by the printEmp procedure.

    Fix by taking display out of the avg procedure:

    (define (avg List)
      (cond ((string=? "std" (car (split List)))
             (/ (examTotals List) 3.0))
            (else 0)))