[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/programming-nu/programming-nu.github.io/master/operators.html [Back]  [Original]

Operators

Nu Operators

Nu includes many useful built-in operators; here is a list of them.

Assignment Operators

set assigns a value to a symbol. Assignments may have global, local, or instance-specific scope, depending on the leading character of the name.

  • Assignments to names beginning with '$' are given global scope. Globally-assigned values are placed in slots in the underlying symbol representation.
  • Assignments to names beginning with '@' are given instance-specific scope. These are instance variables.
  • All other assignments have local scope. Locally-scoped assignments are maintained in dictionaries (instances of NSMutableDictionary) that are called contexts.
(set x 22)

global assigns a value to a symbol with a forced global scope. This allows global values to be set for names that do not begin with '$'.

(global Foo "foo")

let creates a new context, makes a specified set of assignments within that context, and then evaluates a sequence of instructions in that same context.

(let ((x 2)
      (y 3))
     (+ x y))

Arithmetic and Logical Operators

Operator Function
+ - * /basic arithmetic
& |bitwise logical
> < >= shift
and or notlogical

Telling truth

Several operators test values for truth. In Nu, any object that is not nil (the empty list) or 0 is considered to be true. The symbol t is also defined as a general-purpose "true" value. The value of t is itself. It is as if we had said:

(global t 't)

List Processing Operators

list evaluates its arguments and constructs a list of the results.

cons evaluates its arguments and creates a new list with the evaluated first argument at the head and the evaluated second argument as the tail.

car and head return the first element of a list.

cdr and tail return the rest of a list after the first element has been removed.

The following example constructs a list that has the same structure as mylist:

(cons (car mylist) (cdr mylist))

The last element of a list is a special value called nil. In Nu, nil is represented by the singleton instance [NSNull null]. The following operations are equivalent:

(list 1 2 3)
(cons 1 (cons 2 (cons 3 nil)))

append joins two lists together to form a new list. The arguments are unchanged.

% (set a '(1 2))
(1 2)
% (set b '(3 4))
(3 4)
% (append a b)
(1 2 3 4)
% a
(1 2)

atom returns true if an element is not a list. (atom nil) also returns false, which is represented with nil. nil is displayed as an empty list, ().

Evaluation Operators

quote prevents the evaluation of its argument. A single quote symbol is an abbreviated synonym for quote.

(quote (1 2 3))
'(1 2 3)

eval causes its argument to be evaluated.

(eval (quote (+ 2 2)))

parse parses a string containing Nu source code into a list structure.

(parse "(+ 2 2)")

context returns the current execution context (an NSMutableDictionary).

Control Flow Operators

Conditional Operators

cond scans through a list of lists, evaluating the first member of each list. When one evaluates true, the remainder of that list is evaluated and the result of the last evaluation is returned. If none evaluate true, the last list in the list of lists is evaluated. Since the last clause is always evaluated, it is conventional but not required for it to begin with the else keyword.

(cond ((eq x 1) (puts "x is 1"))
      ((eq x 2) (puts "x is 2"))
      (else (puts "x is something else")))

case tests an expression against a sequence of values, each at the head of a list. When the expression matches a value, the rest of that value's list is evaluated and the result of the last evaluation is returned. If none of the values match, the last list in the list of lists is evaluated. Since the last clause is always evaluated, it is conventional but not required for it to begin with the else keyword.

(case  x
      (1 (puts "x is 1"))
      (2 (puts "x is 2"))
      (else (puts "x is something else")))

if tests an expression. If it evaluates true, the rest of the expressions that follow are evaluated, except for any expressions in a list beginning with the else symbol. These expressions will be evaluated if the expression evaluates false.

(if (eq x 1) 
    (puts "x is 1")
    (puts "i'm sure it is")
    (else 
         (puts "x is not 1")))

As a convenience, expressions can be grouped into lists that begin with the then symbol.

(if (eq x 1)
    (then (puts "x is 1"))
    (else (if (eq x 2) 
                (then (puts "x is 2"))
                (else (puts "x is something else")))))

unless can be seen as the opposite of the if operator.
unless tests an expression. If it evaluates false, the rest of the expressions that follow are evaluated, except for any expressions in a list beginning with the else symbol. These expressions will be evaluated if the expression evaluates true.

Looping Operators

while tests an expression. If it evaluates true, the rest of the expressions that follow are evaluated. Then the expression is tested again and evaluations continue until the expression evaluates to false. The following while expression prints the numbers from 1 to 10:

(set i 1)
(while (



Web Proxy Viewer  |  New URL  |  Original Page