Change itwhile it runs.
Diaspar is a compiled Lisp with a live REPL. Define a function again, and the running program uses it at the next call.
Open source soon. Diaspar is in development, and its source is not public yet.
(defn greet [name] f"hi, {name}")
Not a recording: Diaspar itself, compiled to WebAssembly, running in this tab. Enter evaluates the line.
Diaspar in five programs
Each one is editable and runs right here. Break them on purpose: the reports are part of the language. The tour has the reasons behind each.
01 · Values
Nothing changes under you
A vector, a map, a record: each is a value, and a value never changes. conj gives a new vector and the old one is as it was, so no function you call can alter what you hold. When nobody else holds the old one, the change is made in place.
Try it: Print crew once more at the end. Still two.
(def crew #["Alvin" "Hilvar"]) (defn board [crew name] (conj crew name)) (defn main [args] (let [bigger (board crew "Alystra")] (print "before:" crew) (print "after: " bigger) ;; Equal means the same contents, however deep. (print (= bigger #["Alvin" "Hilvar" "Alystra"])) ;; Money that adds up. (print (+ 0.1 0.2) "but" (+ 0.1dec 0.2dec))))
02 · Data
Enums, and a match that knows the whole list
An enum lists the shapes a value may take, and a pattern looks like the code that builds one. This match forgot a variant. Diaspar names it before the program starts, and runs the program anyway.
Try it: Add the clause the warning asks for, or _ 0.
(defenum Shape (Circle [r]) (Rect [w h]) (Triangle [base height])) (defn area [shape] (match shape (Shape.Circle r:) (* 3.14159 r r) (Shape.Rect w: h:) (* w h))) (defn main [args] (for [shape #[(Shape.Circle r: 1) (Shape.Rect w: 2 h: 3)]] (print f"{shape} has area {(area shape)}")))
03 · Diagnostics
Reports that name the fix
Every error has a place, the source with the spot underlined, and what the language knows about it: here, the fields the record does have. The place is inside a string, and the report still finds it.
Try it: Fix the field's name on line 4.
Tour: Records(deftype Ship [name seats]) (defn describe [ship] f"{ship.name} seats {ship.saets}") (defn main [args] (print (describe (Ship name: "Seeker" seats: 3))))
04 · Effects
A function asks; its caller decides
config needs two answers and does not know where they come from. The caller's handler gives them: a form, a file, a test. Errors go the same way, so a caller can catch one, or answer it and let the work go on from where it failed.
Try it: In the first handler, answer the port with "eighty".
(defeffect Ask [question]) (defn parse-port [text] (if (= text "8080") 8080 (error f"not a port: {text}"))) (defn config [] #{:host (perform (Ask "host")) :port (parse-port (perform (Ask "port")))}) (defn main [args] ;; The caller answers the questions. (print (handle (config) (Ask "host") [resume] (resume "localhost") (Ask "port") [resume] (resume "8080"))) ;; An error is a question too: "what now?" This caller says: use 80. (print (handle (config) (Ask _) [resume] (resume "?") (Fail f) [resume] (resume 80))))
05 · Text
Strings that count what you see
A string is a sequence of what a reader calls characters, whatever they are made of. A family is one, a flag is one. F-strings hold any expression, with Python's formats, and widths that line up in any script.
Try it: Add a name with an emoji to crew.
(def crew #["Alvin" "Hilvar" "Zoë" "明"]) (defn main [args] ;; count is what a reader sees: one family, one flag. (print (count "👨👩👧") (count "🇦🇲") (count "Zoë")) ;; Widths count columns on the screen, so the table lines up. (for [name crew] (print f"|{name:<8}|{(count name):>3}|")) (print f"{1234567.891:,.2f} {0.256:.1%} {(count crew)=}"))