diff --git a/_posts/2022-10-13-lisp.md b/_posts/2022-10-13-lisp.md new file mode 100644 index 0000000..f2a13cf --- /dev/null +++ b/_posts/2022-10-13-lisp.md @@ -0,0 +1,71 @@ +--- +layout: post +title: Lisp (Clojure) +date: 2022-10-13 16:28:00+0200 +categories: programming-language +--- + +# Lisp + +Lisp is a family of programming languages with many dialects in it. + +Most popular are Clojure, Common Lisp, Racket. + +## Syntax + +There are a few special symbols: ` ()'"[]`. + +You can write numbers and strings as you usually do in other programming +languages. + + +```clojure +100500 ; comment about integer +-1.5 ; flating point number +22/7 ; ratio +"ruby" ; string +\e ; character +#"\d" ; regex +``` + +## Symbols + +```clojure +def ; symbol ++ ; symbol +java.lang.Math/PI ; namespaced symbol +:with ; keyword +:with/regex ; keyword with namespace +``` + +## Literal collections + +```clojure +'(1 2 3) ; list +[1 2 3] ; vector +#{1 2 3} ; set +{:a 1, :b 2} ; map +``` + +### Atoms + +Atom is either a literal (numeric or string) of or a symbol. +You can think of a symbol as of name. + +```clojure +(defn factorial [n] + (if (< n 2) + 1 + (* n (factorial (- n 1))))) +``` + +### S-expressions + +Symbolic expression is an atom or list of other s-exrpressions in braces. + +```clojure +(list 3 4 5 (* 3 2) 7) ; s-expression +; list and * are symbols +; 2 3 4 5 7 - numbers +; (* 3 2) - s-expression +```