案例陈述未赋值
我在调试 case 语句时遇到一些问题。我希望该语句将为 note-val
分配数值,但到目前为止它正在分配 #
。我知道 case 语句有问题,因为如果我添加 else 子句,就会应用该值。给定 '(((#\3 #\A) (#\4 #\B)) ((#\4 #\C)))
的示例输入,我在这里做错了什么? (关于 case 语句。我确信还有其他错误,但如果我能解决这个问题,我想尝试自己解决这些错误。)
(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
等)。
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))
'()))))
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
case
使用eqv?
进行匹配。这意味着除了符号、数字、字符、布尔值或空列表之外的任何内容都不会匹配。就您而言,您试图匹配(非空)列表。那永远不会起作用。 :-( (匹配字符串或向量都不起作用。)
case
does matching usingeqv?
. 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.)
添加到之前的答案(以允许我添加代码的方式):如果您使用 Racket,您可能对“匹配”形式感兴趣:
第二个测试用例说明了如果您是真正与“字符列表”表示结合在一起。
To add to the prior answer (in a way that allows me to add code): if you're using Racket, you might be interested in the "match" form:
The second test case illustrates how you might use this if you're really married to the "list-of-chars" representation.