Walden programming language
C Makefile C++ Other
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


;; This is a basic overview of the language in action

;; This is a comment
;; Any text that comes after a semi-colon (';') until a newline ('\n')
;; character is treated as white-space by the compiler.
; Comments
;;;; Comments
;;; must begin
;; with least
; one semicolon

;; '(' '[' '{' all mean the same thing
(var o 0)
[var p 1]
{var v 2}

;; Define a constant variable that cannot be modified past definition.
;; In this case, x is bound to the value 42. The colon is special syntax to
;; designate the type. Read as x is a member of the set of Integers.
(def x : Int 42)

;; Define a function that cannot be re-defined. The sequence following the
;; colon (':') character specifies the type of the function. This example of
;; (-> Int Int) reads as "A function that receives and Integer and returns an
;; Integer. The body of the function adds 42 to it's single parameter
(def (f x) : (-> Int Int) (+ x 42))

;; This is an anonymous function, or a lambda. The type annotation is slightly
;; different from 'def' so that types may be inferred in certain contexts.
(\ (x : Int) : Int (+ x 42))

;; 'def' for function definitions is defined in terms of lambda.
(def (f x) : (-> Int Int) (+ x 42))
;; The lambda version:
(def f : (-> Int Int) (\ (x) (+ x 42)))

;; 'var' declares local storage space. Think of it as a mutable 'def' for
;; variables only. Note that type annotations can be omitted with 'var' if
;; the type can be inferred.
(var y 0)
;; This will change y's value to 5.
(set! y 5)

;; If expressions branch to the 'then' expression if the condition expression
;; is true, otherwise the 'else' expression is branched to. If returns whatever
;; value was evaluated in the branch branched to.
(if (< 5 6) ;; Condition
    true    ;; Then expression
    false)  ;; Else expression

;; Do blocks evaluate a list of expressions and return the value of the last
;; one. This 'do' expression will return 0. Useful to execute many expressions
;; in a place where only one is allowed, such as if expressions.
(do (print "hello")
    (print "world")

;; While loops loop until the condition expression evaluates to false. They
;; always return 'nil' the unit type value.
(var i 0)
(while (< i 10)
       (print i)
       (set! i (+ i 1)))

;; Let allows for temporary local bindings as a shorthand for an instantly
;; called lambda function.
(let [x 0
      y 5]
  (+ x y))

;; The equivalent lambda expression for the above 'let' is below.
;; The lambda is defined and instantly called with arguments.
((\ (x y) (+ x y)) 0 5)

;; Almost all 'keywords' are defined in terms of lambda (except for 'if' and 'set!')!

;; Below is the definition of a new data type and functions using it.
(type MyList
  [data : Int]
  [next : @MyList])

;; Predicate to determine if there is another element in the list after the
;; current one
(def (next? lp) : (-> @MyList Bool)
  (if (nil != lp.next)

;; Function that simply returns the next element
(def (next lp) : (-> @MyList @MyList)