案例陈述未赋值

发布于 2024-11-03 05:27:06 字数 1247 浏览 1 评论 0原文

我在调试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扬花落满肩 2024-11-10 05:27:06

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

如何视而不见 2024-11-10 05:27:06

添加到之前的答案(以允许我添加代码的方式):如果您使用 Racket,您可能对“匹配”形式感兴趣:

#lang racket

(require rackunit)

(define (pitchclass->half-steps pitchclass)
  (match pitchclass
    [(or "B#" "C")   0]
    [(or "C#" "Db")  1]
    ["D"             2]
    [(or "D#" "Eb")  3]
    [(or "E" "Fb")   4]
    [(or "E#" "F")   5]
    [(or "F#" "Gb")  6]
    ["G"             7]
    [(or "G#" "Ab")  8]
    ["A"             9]
    [(or "A#" "Bb") 10]
    [(or "B" "Cb")  11]))

;; TEST CASES:

(check-equal? (pitchclass->half-steps "Ab") 8)
(check-equal? (pitchclass->half-steps (apply string '(#\D #\#))) 3)

第二个测试用例说明了如果您是真正与“字符列表”表示结合在一起。

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:

#lang racket

(require rackunit)

(define (pitchclass->half-steps pitchclass)
  (match pitchclass
    [(or "B#" "C")   0]
    [(or "C#" "Db")  1]
    ["D"             2]
    [(or "D#" "Eb")  3]
    [(or "E" "Fb")   4]
    [(or "E#" "F")   5]
    [(or "F#" "Gb")  6]
    ["G"             7]
    [(or "G#" "Ab")  8]
    ["A"             9]
    [(or "A#" "Bb") 10]
    [(or "B" "Cb")  11]))

;; TEST CASES:

(check-equal? (pitchclass->half-steps "Ab") 8)
(check-equal? (pitchclass->half-steps (apply string '(#\D #\#))) 3)

The second test case illustrates how you might use this if you're really married to the "list-of-chars" representation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文