match1.scm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. (format #t "to string: ~w~%"
  2. (map (lambda (x) (if (? s ::string x) s "N/A")) '(3 "hello" 3.5 #\?)))
  3. ;; Output: to string: ("N/A" "hello" "N/A" "N/A")
  4. (format #t "to String: ~w~%"
  5. (map (lambda (x) (if (? s ::String x) s "N/A")) '(3 "hello" 3.5 #\?)))
  6. ;; Output: to String: ("3" "hello" "3.5" "'?'")
  7. (format #t "to character: ~w~%"
  8. (map (lambda (x) (if (? c ::character x) c #\?))
  9. (list #!eof 3 "hello" #\X (java.lang.Character #\Y))))
  10. ;; Output: to character: (#\? #\? #\? #\X #\Y)
  11. (format #t "to character-or-eof: ~w~%"
  12. (map (lambda (x) (if (? c ::character-or-eof x) c #\?))
  13. (list #!eof 3 "hello" #\X (java.lang.Character #\Y))))
  14. ;; Output: to character-or-eof: (#!eof #\? #\? #\X #\Y)
  15. (format #t "to char: ~w~%"
  16. (map (lambda (x) (if (? c ::char x) c #\?))
  17. (list #!eof 3 #\X (java.lang.Character #\Y) #\x12345)))
  18. ;; Output: to char: (#\? #\? #\X #\Y #\?)
  19. (format #t "to boolean: ~w~%"
  20. (map (lambda (x) (if (? b::boolean x) b -1)) '(1 #f #\X #t)))
  21. ;; Output: to boolean: (-1 #f -1 #t)
  22. (define (to-v o)
  23. (if (? v ::vector o) v '#()))
  24. (format #t "to vector: ~w~%"
  25. (map to-v '(4 (5) #(6))))
  26. ;; Output: to vector: (#() #() #(6))
  27. (define (to-f64v o)
  28. (if (? f64v ::f64vector o) f64v '#f64(-1 0)))
  29. (format #t "to f64v: ~w~%"
  30. (map to-f64v '(4 #(5) #f64(7))))
  31. ;; Output: to f64v: (#f64(-1.0 0.0) #f64(-1.0 0.0) #f64(7.0))
  32. (define (to-list o)
  33. (if (? l ::list o) l '()))
  34. (format #t "to list: ~w~%"
  35. (map to-list '(4 #(5 6) (7 6))))
  36. ;; Output: to list: (() () (7 6))
  37. (define (to-FVector o)
  38. (if (? v ::gnu.lists.FVector o) v '#()))
  39. (format #t "to FVector: ~w~%"
  40. (map to-FVector '(4 (5) #(6))))
  41. ;; Output: to FVector: (#() #() #(6))
  42. (define (to-F64Vector o)
  43. (if (? f64v ::gnu.lists.F64Vector o) f64v '#f64(1 2)))
  44. (format #t "to F64Vector: ~w~%"
  45. (map to-f64v '(4 #(5) #f64(7))))
  46. ;; Output: to F64Vector: (#f64(-1.0 0.0) #f64(-1.0 0.0) #f64(7.0))
  47. (define (to-LList o)
  48. (if (? l ::gnu.lists.LList o) l '()))
  49. (format #t "to LList: ~w~%"
  50. (map to-LList '(4 #(5 6) (7 6))))
  51. ;; Output: to LList: (() () (7 6))
  52. (define (to-list2 o)
  53. (if (? [a b] o) (format "[a:~w b:~w]" a b) (format "no-match:~w" o)))
  54. (display (format #f "to list2: ~a"
  55. (map to-list2 (list 12 #(4 5) #(9) '(m n) '(m n o) '())))) (newline)
  56. ;; Output: to list2: (no-match:12 [a:4 b:5] no-match:#(9) [a:m b:n] no-match:(m n o) no-match:())
  57. (define (to-list2+1 o)
  58. (if (? [[a b] c] o) (format "[a:~w b:~w c:~w]" a b c)
  59. (format "no-match:~w" o)))
  60. (display (format #f "to list2+1: ~a"
  61. (map to-list2+1 (list 12 #((4 5) 7) #(9 8) '(#(p q) r) '(m n o) '()))))
  62. (newline)
  63. ;; Output: to list2+1: (no-match:12 [a:4 b:5 c:7] no-match:#(9 8) [a:p b:q c:r] no-match:(m n o) no-match:())
  64. (display (format "to list2+1-ignore1: ~a"
  65. (map (lambda (o) (if (? [[a _] b] o) (format "[a:~w b:~w]" a b)
  66. (format "no-match:~w" o)))
  67. (list 12 #((4 5) 7) #(9 8) '(#(p q) r) '(m n o) '()))))
  68. (newline)
  69. ;; Output: to list2+1-ignore1: (no-match:12 [a:4 b:7] no-match:#(9 8) [a:p b:r] no-match:(m n o) no-match:())
  70. (display (format "to list2-int: ~a"
  71. (map (lambda (o) (if (? [a::integer b] o) (format "[a:~w b:~w]" a b)
  72. (format "no-match:~w" o)))
  73. (list 12 #((4 5) 7) #(9 8) '(#(p q) r) '(m n) '()))))
  74. (newline)
  75. ;; Output: to list2-int: (no-match:12 no-match:#((4 5) 7) [a:9 b:8] no-match:(#(p q) r) no-match:(m n) no-match:())
  76. (display (format "match#1: ~a"
  77. (map (lambda (o)
  78. (match o
  79. (x::integer (list 'integer x))
  80. (x::double (list 'double x))
  81. (_ (list 'other o))))
  82. '(3.4 12 sym))))
  83. (newline)
  84. ;; Output: match#1: ((double 3.4) (integer 12) (other sym))
  85. (display (format "match#2: ~a"
  86. (map (lambda (o)
  87. (match o
  88. (x::integer #!if (> x 0) (list 'pos-integer x))
  89. (x::integer #!if (< x 0) (list 'neg-integer x))
  90. (0 (list 'zero-integer))
  91. ('(a b) (list 'list o))
  92. (x::double (list 'double x))
  93. (_ (list 'other o))))
  94. '(3.4 0.0 12 (a b) (a c) "str" -3 0))))
  95. (newline)
  96. ;; Output: match#2: ((double 3.4) (double 0.0) (pos-integer 12) (list (a b)) (other (a c)) (other str) (neg-integer -3) (zero-integer))