Advertisement
triclops200

ChordRef so far

Jan 5th, 2013
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 2.68 KB | None | 0 0
  1. (load "./std.scm")
  2. (define A 1)
  3. (define B 2)
  4. (define C 3)
  5. (define D 4)
  6. (define E 5)
  7. (define F 6)
  8. (define G 7)
  9. (define sharp-wheel (list F C G D A E B))
  10. (define flat-wheel (list B E A D G C F))
  11. (define sharp 1)
  12. (define flat 0)
  13.  
  14. (define get-note-name
  15.   (lambda (note)
  16.     (case note
  17.       ((1) "A")
  18.       ((2) "B")
  19.       ((3) "C")
  20.       ((4) "D")
  21.       ((5) "E")
  22.       ((6) "F")
  23.       ((7) "G"))))
  24.  
  25. (define get-keytype-name
  26.   (lambda (type)
  27.     (case type
  28.       ((1) "sharp")
  29.       ((0) "flat"))))
  30.  
  31. (define get-accidents
  32.   (lambda (n xs)
  33.     (if (= n 0)
  34.     (list)
  35.     (cons (car xs) (get-accidents (- n 1) (cdr xs))))))
  36.  
  37. (define num-sharps
  38.   (lambda (key)
  39.     (modulo (+ (* key 2) 1) 7)))
  40.  
  41. (define num-flats
  42.   (lambda (key)
  43.     (case key
  44.       ((1) 4)
  45.       ((2) 2)
  46.       ((3) 0)
  47.       ((4) 5)
  48.       ((5) 3)
  49.       ((6) 1)
  50.       ((7) 6))))
  51.  
  52. (define contains?
  53.   (lambda (x xs)
  54.     (if (null? xs)
  55.     #f
  56.     (if (eq? x (car xs))
  57.         #t
  58.         (contains? x (cdr xs))))))
  59.  
  60. (define in? contains?)
  61. (define get-sharps (lambda (key) (get-accidents (num-sharps key) sharp-wheel)))
  62. (define get-flats (lambda (key) (get-accidents (num-flats key) flat-wheel)))
  63.  
  64. (define flat-key?
  65.   (lambda (key)
  66.     (if (and (!= key F) (!= key C))
  67.     "b"
  68.     " ")))
  69.  
  70. (define sharp-key?
  71.   (lambda (key)
  72.     (if (= key F)
  73.     "#"
  74.     " ")))
  75.  
  76. (define get-accs
  77.   (lambda (key type)
  78.     (if (= type sharp)
  79.     (get-sharps key)
  80.     (get-flats key))))
  81.  
  82. (define key-suffix
  83.   (lambda (key type)
  84.     (if (= type sharp)
  85.     (sharp-key? key)
  86.     (flat-key? key))))
  87.  
  88. (define parse-input
  89.   (lambda (stri)
  90.     (let ((str (substring stri 0 2)))
  91.       (cond
  92.     ((string=? str "C ")  (list C sharp))
  93.     ((string=? str "D ")  (list D sharp))
  94.     ((string=? str "E ")  (list E sharp))
  95.     ((string=? str "F ")  (list F flat))
  96.     ((string=? str "G ")  (list G sharp))
  97.     ((string=? str "A ")  (list A sharp))
  98.     ((string=? str "B ")  (list F sharp))
  99.     ((string=? str "Db")  (list D flat))
  100.     ((string=? str "Eb")  (list E flat))
  101.     ((string=? str "F#")  (list F sharp))
  102.     ((string=? str "Gb")  (list G flat))
  103.     ((string=? str "Ab")  (list A flat))
  104.     ((string=? str "Bb")  (list B flat))))))
  105.  
  106. (define fix-input
  107.   (lambda (str)
  108.     (if (= (string-length str) 1)
  109.     (string-append str " ")
  110.     str)))
  111.  
  112. (define main-display
  113.   (lambda (KEY KEYTYPE)
  114.     (begin
  115.       (display (string-capitalize (string-append (get-keytype-name KEYTYPE) "s")))
  116.       (display " in the key of ")
  117.       (display (get-note-name KEY))
  118.       (display (key-suffix KEY KEYTYPE))
  119.       (display ": ")
  120.       (display (map-reduce get-note-name (s-app-gen ",") (get-accs KEY KEYTYPE)))
  121.       (newline)
  122. )))
  123.  
  124.  
  125. (apply main-display (parse-input (fix-input (readline))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement