【问题标题】:#!unspecific error while printing values in Scheme#! 在 Scheme 中打印值时出现特定错误
【发布时间】:2020-08-10 06:15:20
【问题描述】:

我一直得到我的输出:

Exam Avg: 50.#!unspecific

每当我尝试在方案中打印我的程序时。我正在使用两个函数 print 和 secprint 我认为可能会发生错误:

(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))
)



【问题讨论】:

  • avgavg2 是什么?
  • 几乎可以肯定#!unspecific 是打印未指定返回值的方式。我不知道为什么它自己不在一条线上,因为我希望它是 newline 的返回值。
  • @molbdnilo avg 和 avg2 是我用来获取考试成绩的函数,我可以编辑原件以包含它们

标签: scheme lisp racket mit-scheme


【解决方案1】:

display 过程写入输出,然后返回一个未指定的值。您使用display 是因为它打印数据的副作用,而不是它的返回值。

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

avg 过程返回最后计算的表达式的值;当display 分支计算时,未指定的值返回给调用者。但是调用者printEmp 不需要avg 来显示其结果,它只需要一个返回值,然后由printEmp 过程打印。

通过将displayavg 过程中取出来进行修复:

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

【讨论】:

    猜你喜欢
    • 2022-01-25
    • 2012-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    相关资源
    最近更新 更多