Commit 2cff7e4e authored by Chris Müller's avatar Chris Müller
Browse files

Add statement example documentation

parent e77c61ab
......@@ -1169,11 +1169,11 @@ environment(void)
proc_to_env(env, "float->number", core_float_to_number);
proc_to_env(env, "string?", core_is_string);
proc_to_env(env, "string-equal?", core_string_equal);
proc_to_env(env, "string->list", core_string_to_list);
proc_to_env(env, "string->tuple", core_string_to_tuple);
proc_to_env(env, "string->number", core_string_to_number);
proc_to_env(env, "string->symbol", core_string_to_symbol);
proc_to_env(env, "string-equal?", core_string_equal);
proc_to_env(env, "symbol?", core_is_symbol);
proc_to_env(env, "symbol->string", core_symbol_to_string);
;; ----------------------------------------------------------------------------------
;; each cherry file represents a module and is specified through its path relative from LOADPATH
;; ----------------------------------------------------------------------------
;; Cherry's control structure- and operations
;; ----------------------------------------------------------------------------
;; Traditional if statements (consequence and alternative entries uses only
;; a single instruction)
(if (< 10 20)
(println "10 is bigger then 20")
(println "Will never be executed"))
;; Cond macro are a simple extensions of the if statement
(when (equal? str "case1")
(println "case1 is there"))
(when (equal? str "case2")
(println "case2 is there"))
(println "nothing")))
;; Cherry also supports a pattern matching macro (a more sophisticated case expression)
(match (get "a pattern")
(when ()
(println "Found an emptylist"))
(when (head . tail)
(println "Extracts head and tail from a list"))
(when (1 2 3)
(println "Found a list (1 2 3)"))
(when [\1 \2 \3 \newline]
(println "Found a tuple for four characters"))
(when N
(println "Save matching result in var n"))
(println "Nothing has matched")))
;; Sequences for operations that only accepts one single instructions
;; Last operations is used for the return value
(println "Operation 1")
(println "Operation 2")
;; Variable declarations are performed with let.
(let PI 3.14)
(let e 2.7182)
;; --> prints "3.14 and 2.7182"
(println PI " and " e)
;; Function declarations can be performed with define macro
(define (fun n) (+ n 1))
;; define macro transform this to the low-level instructions
(let fun (lambda (n) (+ n 1)))
;; Cherry is a functional language and uses recursion for iteration
;; e.g. you can define a tail-recursive function:
(define (factorial n)
(define (fact acc n)
(if (> n 1)
(fact (* acc n) (- n 1))
(fact 1.0 n))
;; Internal functions can also be expressed in form of the loop instruction
;; Loop is simple syntax sugar which is equivalent to the upper form
;; (In Scheme also known as named let)
(define (factorial n)
(loop (fact (acc 1.0) (n n))
(if (> n 1)
(fact (* acc n) (- n 1))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment