02 / 10
Values
Numbers, vectors, maps and sets: values that never change under you.
Everything a Diaspar program works with is a value: a number, a string, a collection of other values. A value never changes. That one rule is most of this page.
Numbers
There are integers, floats and decimals. Arithmetic is written as a call, like everything else: (+ 1 2).
(defn main [args] ;; Integers are 64 bits wide. Floats are doubles. (print (+ 1 2) (* 6 7) (- 10 2.5)) ;; / always divides exactly. quot and rem are for integers. (print (/ 7 2) (quot 7 2) (rem 7 2)) ;; A number is equal to itself, however it is written. (print (= 1 1.0) (< 1 2 3) (= 0.1 0.10)) ;; Floats are binary. Decimals are exact, for money. (print (+ 0.1 0.2)) (print (+ 0.1dec 0.2dec)))
Try it: Compare (= 0.3 (+ 0.1 0.2)) with (= 0.3dec (+ 0.1dec 0.2dec)).
/divides exactly, so(/ 7 2)is3.5. For whole division there arequotandrem.- There is one
=. It compares numbers by what they are worth, so(= 1 1.0)is true. - A number that ends in
decis a decimal. It is exact, and it is the kind to use for money.
An integer has 64 bits. When a result does not fit, that is an error. It is never a wrong number.
(def biggest 9223372036854775807) (defn main [args] (print "the biggest integer:" biggest) (print (+ biggest 1)))
Try it: Change + to -. The program finishes.
Collections
Three collections are written with a # before the bracket. A vector, #[ ], keeps its elements in order. A map, #{ }, goes from keys to values. A set, #set[ ], holds each element once.
(def crew #["Alvin" "Hilvar" "Alystra"]) ; a vector (def ship #{:name "Seeker" :seats 3}) ; a map (def visited #set[:lys :diaspar]) ; a set (defn main [args] (print (count crew) (first crew) (last crew) (nth crew 1)) (print ship.name (get ship :seats) (get ship :captain "nobody")) (print (contains? visited :lys) (contains? visited :shalmirane)) ;; A collection prints the way it is written. (print crew) (print ship) (print visited))
Try it: Read a key the map does not have with a dot: ship.captain. The error names the key. get is for a key that may be absent.
A name such as :name is a keyword: a value that stands for itself, and the usual key of a map. A dot reads a key that must be there, and get reads one that may not be.
A value never changes
conj adds an element and assoc sets a key. Neither touches the collection it was given. Each gives you a new one, and the old one is still what it was.
(def crew #["Alvin" "Hilvar"]) (def ship #{:name "Seeker" :seats 3}) (defn main [args] ;; conj and assoc give a new value. The old one is as it was. (let [bigger (conj crew "Alystra") refit (assoc ship :seats 5)] (print crew) (print bigger) (print ship) (print refit)))
Try it: Add (print (assoc refit :name "Ark")) at the end. Three ships now, and none of them changed another.
So a value you hold is yours. No function you pass it to can change it, and no other part of the program can change it while you look away. You never copy a collection to be safe.
Equal, true, nothing
= compares what two values hold, as deep as they go. Two vectors with the same elements are equal, wherever they came from.
(defn main [args] ;; = compares what two values hold, however deep. (print (= #[1 2 3] #[1 2 3])) (print (= #{:a 1 :b #[2 3]} #{:b #[2 3] :a 1})) (print (= #[1 2 3] '(1 2 3))) (print (= "1" 1)) ;; Only nil and false count as false. (print (if 0 "yes" "no") (if "" "yes" "no") (if nil "yes" "no")) ;; nil is nothing, and asking nothing for something gives nil. (print (get nil :name) (first #[]) (count nil)))
Try it: What is (= nil false)? And (count "")?
- Only
nilandfalsecount as false. Zero and the empty string are values like any other, and count as true. nilis the one value for "nothing here". Asking nothing for something gives nothing, so(get nil :name)isnil.
Text is a value too, with rules of its own: the next page.