Values in Clojure
Clojure has various value types including strings, integers, floats, booleans, etc. Here are a few basic examples.
(ns values-example
(:require [clojure.core :refer :all]))
(defn main []
; Strings, which can be concatenated with str
(println (str "clojure" "lang"))
; Integers and floats
(println "1+1 =" (+ 1 1))
(println "7.0/3.0 =" (/ 7.0 3.0))
; Booleans, with boolean operators as you'd expect
(println (and true false))
(println (or true false))
(println (not true)))
(main)To run the program, save it as values.clj and use the clojure command:
$ clojure values.clj
clojurelang
1+1 = 2
7.0/3.0 = 2.3333333333333335
false
true
falseIn Clojure, we don’t need to explicitly declare a package or import statements like in some other languages. Instead, we define a namespace using ns and can require other namespaces as needed.
Clojure uses prefix notation for arithmetic and logical operations. For example, (+ 1 1) instead of 1 + 1.
String concatenation in Clojure is typically done with the str function, which can take any number of arguments.
Clojure’s printing function is simply println, which automatically adds a newline after printing.
Boolean operations in Clojure are similar to other languages, using and, or, and not.
This example demonstrates basic value types and operations in Clojure, providing a foundation for understanding the language’s syntax and functionality.