案例陈述未赋值 [英] Case Statement Not Assigning Value
问题描述
我在调试 case 语句时遇到了一些问题.我希望该语句将数值分配给 note-val
,但到目前为止它正在分配 #<void>
.我知道 case 语句有问题,因为如果我添加 else 子句,就会应用该值.给定 '(((#\3 #\A) (#\4 #\B)) ((#\4 #\C)))
的示例输入,我在这里做错了什么?(关于 case 语句.我确定还有其他错误,但如果我能解决这个问题,我想尝试自己解决.)
I'm having some trouble debugging a case statement. I was hoping that the statement would assign numeric values to note-val
, but so far it is assigning #<void>
. I know it's something wrong with the case statement, because if I add an else clause, that value gets applied. Given a sample input of '(((#\3 #\A) (#\4 #\B)) ((#\4 #\C)))
, what am I doing wrong here? (In regards to the case statement. I'm sure there are other errors, but I'd like to try to work those out myself if I can get this fixed.)
(define (calc-freqs chord)
(let ((octave (char->int (caaar chord)))
(note-val (case (cdaar chord)
[((#\B #\#) (#\C)) 0]
[((#\C #\#) (#\D #\b)) 1]
[((#\D)) 2]
[((#\D #\#) (#\E #\b)) 3]
[((#\E) (#\F #\b)) 4]
[((#\E #\#) (#\F)) 5]
[((#\F #\#) (#\G #\b)) 6]
[((#\G)) 7]
[((#\G #\#) (#\A #\b)) 8]
[((#\A)) 9]
[((#\A #\#) (#\B #\b)) 10]
[((#\B) (#\C #\b)) 11])))
(cons (* a4 (expt 2 (+ (- octave 4) (/ (- note-val 9) 12))))
(if (pair? (cdr chord))
(calc-freqs (cdr chord))
'()))))
哦,还有 char->int
是我写的一个很小的实用函数,它几乎完全符合它的名称 (#\1
=> 1
,依此类推).
Oh, and char->int
is a tiny utility function I wrote that pretty much does what it says in the name (#\1
=> 1
, and so on).
推荐答案
case
使用 eqv?
进行匹配.这意味着除了符号、数字、字符、布尔值或空列表之外的任何东西都不会匹配.
case
does matching using eqv?
. That means that anything other than symbols, numbers, characters, booleans, or the empty list will never match.
就您而言,您试图匹配(非空)列表.那永远行不通.:-((匹配字符串或向量都不起作用.)
In your case, you were trying to match (non-empty) lists. That will never work. :-( (Neither will matching strings or vectors work.)
这篇关于案例陈述未赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!