-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.lisp
More file actions
131 lines (110 loc) · 3.11 KB
/
example.lisp
File metadata and controls
131 lines (110 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
(asdf:operate 'asdf:load-op 'cl-morse)
(use-package 'cl-morse)
(defmacro print-each (&body body)
`(progn ,@(mapcar (lambda (sexp) `(print ,sexp)) body)))
(defmacro print-2-values (sexp)
`(multiple-value-bind (a b) ,sexp
(print a)
(print b)))
(defun heading (s)
(format t "~&=== ~a ===" s))
;;; Different types handled.
(heading "parse-morse, basic")
(print-each
;; FOO
(parse-morse '..-.=---=---)
;; #\A
(parse-morse 'C.-)
;; 42
(parse-morse 'N....-=..---)
;; "FOO BAR"
(parse-morse "..-.=---=--- -...=.-=.-.")
;; (LET ((FOO "BAR"))
;; (PRINT FOO))
(parse-morse '(.-..=.=- ((..-.=---=--- "-...=.-=.-."))
(.--.=.-.=..=-.=- ..-.=---=---))))
;;; The separator may be any character.
(heading "parse-morse, other separators")
(print-each
;; FOO
(parse-morse '..-._---_---)
(parse-morse '|..-. --- ---|)
;; "FOO BAR"
(parse-morse "..-. --- --- -... .- .-.")
(parse-morse "..-./---%---xy-..._.-+.-."))
;;; Executing morse code.
;;; Prints "FOO", then "BAR".
(heading "morse-code")
(morse-code
(.--.=.-.=..=-.=- "..-.=---=---")
(.-..=.=- ((..-.=---=--- "-...=.-=.-."))
(.--.=.-.=..=-.=- ..-.=---=---)))
;;; The other way around
(heading "to-morse")
(print-each
(to-morse 'foo)
(to-morse #\a)
(to-morse 42)
(to-morse "foo bar")
(to-morse '(let ((foo "bar"))
(print foo))))
;;; Convenient when transforming code.
;;; Prints the code executed above.
(heading "morsify")
(print-2-values
(morsify
(print "foo")
(let ((foo "bar"))
(print foo))))
;;; Other separators can be used.
(heading "morsify-with")
(print-2-values
(morsify-with (#\_)
(print "foo bar")
(print "a b")))
;;; Even for the word separator.
(heading "morsify-with, word separator")
(print-2-values
(morsify-with (#\_ "///")
(print "foo bar")
(print "a b")))
;;; For everyday use.
(heading "string->morse")
(print-each
;;; "..-. --- --- -... .- .-."
(string->morse "foo bar")
;;; "..-. --- --- -... .- .-."
(string->morse "foo bar" 4))
;;; "FOO BAR"
(heading "morse->string")
(print-each
(morse->string "..-. --- --- -... .- .-.")
(morse->string "..-.=---=---___-...=.-=.-."))
;;; And now for the cool part: Morse mode!
;;; By default, m and M are set as macro characters.
(heading "enable-morse-mode, default")
(enable-morse-mode)
m(.--.=.-.=..=-.=- "..-.=---=---")
M(.-..=.=- ((..-.=---=--- "-...=.-=.-."))
(.--.=.-.=..=-.=- ..-.=---=---))
;;; Even better
MORSE
(.--.=.-.=..=-.=- "..-.=---=---")
(.-..=.=- ((..-.=---=--- "-...=.-=.-."))
(.--.=.-.=..=-.=- ..-.=---=---))
...-.- ; End symbol
;;; (disable-morse-mode) contains an m and cannot be called directly.
m(-.._.._..._.-_-..._.-.._._-....-_--_---_.-._..._._-....-_--_---_-.._.)
;;; Less radical: Specify a morse-word and, optionally, an end symbol
(heading "enable-morse-mode, custom")
(enable-morse-mode "#tada" :end)
#t(.--.=.-.=..=-.=- "..-.=---=---")
#tada
(.--.=.-.=..=-.=- "..-.=---=---")
(.-..=.=- ((..-.=---=--- "-...=.-=.-."))
(.--.=.-.=..=-.=- ..-.=---=---))
:end
(disable-morse-mode)
(enable-morse-mode)
morse
(....=.=.-=-..=..=-.=--. "-=....=. .=-.=-..")