Skip to the content
Diaspar

01 / 10

Hello, and the REPL

A program, the function it starts at, and a session you can change while it runs.

A Diaspar program is a file with a function named main. Here is the smallest one. It runs right here: the language itself is compiled to WebAssembly, and your browser runs it.

hello.dias
(defn main [args]
  (print "Hello from Diaspar."))
Runs in your browser

Try it: Give print more to say: (print "Hello" 42 :ok). It writes its values with a space between them.

diaspar run loads the file and calls main with the program's arguments. defn defines a function: its name, its parameters in square brackets, then its body.

Everything is a form

Code is made of forms. A form is a list in brackets, and its first element says what the form does: (print "hi") calls print, (+ 1 2) calls +, (if a b c) chooses. There are no statements and no operators with their own rules. A form has a value, so a form can stand anywhere a value can.

forms.dias
;; A form is a list in brackets. Its first element says what it does.
(def crew #["Alvin" "Hilvar" "Alystra"])

(defn greet [name]
  f"hi, {name}")

(defn main [args]
  (print (greet (first crew)))
  (print (+ 1 2 3) (* 2 (+ 3 4)))
  (print (if (> (count crew) 2) "a crowd" "a few")))
Runs in your browser

Try it: Add a fourth name to crew, or change the 2 in the last line.

Round brackets are for calls. Square brackets hold parameters and other lists of names. With a # in front, brackets write data: #[1 2 3] is a vector, and Values has the rest.

One print

print is the only function that writes. By default it puts a space between its values and ends the line. Labels change that: sep: is what goes between the values, and end: is what goes after them.

print.dias
(defn main [args]
  (print "sand" "water" "sky")
  (print "sand" "water" "sky" sep: " / ")
  (print "no new line, " end: "")
  (print "then the rest")
  (print "a number:" 42 "a keyword:" :ready "nothing:" nil))
Runs in your browser

Try it: Add to: :stderr to one of the calls. Its line moves to the second part of the output, which shows what the program wrote to standard error.

When it goes wrong

This program multiplies a string by a number. Run it. The report gives the place, the values that were there, and the call that led to it.

oops.dias
(defn total [price qty]
  (* price qty))

(defn main [args]
  (print "before")
  (print (total "12" 3))
  (print "after"))
Runs in your browser

Try it: Change "12" to 12. Both lines after before are printed, and the program ends with exit 0.

Every report has this shape: what happened, where, the source with the place underlined, then notes. When there is an obvious fix, a help: line says it. You will meet many of them on this tour, on purpose.

The REPL

A file is one way to run Diaspar. The other is a session: you type a form, it is evaluated, and its value is printed. The session below is real. Press Enter to evaluate what is on the line, step by step, or type your own.

diaspar replnot started
(+ 1 2)

1/7A form is evaluated when you press Enter, and its value is printed.

The session keeps everything you define. So you can build a program in pieces, call any piece by itself, and define it again when it is wrong, while everything else stays as it is. The page on names shows what that does to a running program.

On your machine

The same two ways, in a terminal. Diaspar is in development and its source is not public yet, so for now this is what you will type, not what you can type.

terminal
diaspar run hello.dias    # load the file and call main
diaspar repl              # start a session