Skip to content

Latest commit

 

History

History
96 lines (70 loc) · 2.95 KB

LANGUAGE.md

File metadata and controls

96 lines (70 loc) · 2.95 KB

The Language

Primitives

Number

5   ;; Number
1/5 ;; Numbers can also be fractional
5/1 ;; 5/1 is the same as 5. Actually, 5 is implemented as 5/1 underneath

Boolean

True  ;; true
False ;; false

String

"Strings are written with double quotes."

Symbols

memes         ;; symbols are defined like this
<oi>          ;; they can start with anything you want, except numbers
m3M3$_irados  ;; after that, they can hold pretty much anything

Pairs

'()             ;; The empty pair (also called Nil) is created by using empty parenthesis.
'(1 . 5)        ;; Pairs can be defined using the dotted syntax.
'(2 3)          ;; Lists are pairs where the second element is also a pair, and are defined by putting anything between parenthesis.
(+ 2 3) => 5    ;; When you evaluate a list, it runs the first element as a function with the remaining elements as arguments.
(cons + '(2 3)) ;; We have all of your typical car, cdr, cons, etc. functions

Defining your things :P

Variables

(def a 5)                                                                  ;; Variables are declared with the def built-in
(def things-i-like (list "memes" "prog rock" "low quality horror movies")) ;; And they can hold pretty much everything ;)

(let (x 2 y 5) (sub x y)) ;; => -3. Local variables are defined like this.
                          ;; The odd positions are the identifiers, the even are the values.

Functions

(defn       ;; Functions can be declared with the defn built-in
  sum       ;; its first argument is the name of your function
  (a b)     ;; the second one is a list with the name of your parameters
  (+ a b))  ;; and the third is your function body 

(defn sub                                     ;; You can define multi-arity functions like this.
  ((x) (negate x))                            ;; And then your function will work according to the number of parameters
  ((x +rest)                                  ;; They can even be variadics
    (append (+ x) (map negate +rest))))       ;; But be careful: you can only have one body per number of params, and one variadic
   

(fn (x) (* 2 x)) ;; Anonymous functions are defined like this
                 ;; They can only have simple or variadic bodies.

Macros

(defmacro         ;; You can define macros with the defmacro builtin
  add
  (+rest)
  (cons + +rest)) 

Dealing with errors

(defn my-failing-operation ()
  (raise 'my-error "Oops, I broke")) ;; Errors can be raised via the raise built-in
  
(defn print-error (error)
  (do (println "Operation failed: ")
      (println error)))

(call-with-error-handler ;; call-with-error-handler can be used to recover from operations that may fail 
  (my-failing-operation) ;; the first parameter is the operation that may fail
  print-error)           ;; The second one is a function that receives the error as a parameter