# NESL Quick Reference

For more information see the NESLManual or the NESL User's Guide.

NESL is not case sensitive. In the syntax below we use uppercase to present literal symbols.

Syntax Example
`FUNCTION name(args) = exp ;` `FUNCTION double(a) = 2*a;`
`IF e1 THEN e2 ELSE e3` `IF (a > 22) THEN a ELSE 5*a`
`LET binding* IN exp` ``` LET a = b*6; IN a + 3 ```
``` {e1 : pattern IN e2} ``` ``` {a + 22 : a IN [2, 1, 9]} ```
``` {pattern IN e1 | e2} ``` ``` {a IN [2, 1, 9] | a < 8} ```
``` {e1 : p1 IN e2 ; p2 in e3} ``` ``` {a + b : a IN [2,1]; b IN [7,11]} ```

Scalar Functions
logical `not or and xor nor nand`
comparison `== /= < > <= >= `
predicates `plusp minusp zerop oddp evenp`
arithmetic ``` + - * / rem abs max min lshift rshift sqrt isqrt ln log exp expt sin cos tan asin acos atan sinh cosh tanh ```
conversion ``` btoi code_char char_code float ceil floor trunc round ```
random numbers ``` rand rand_seed ```
constants ``` pi max_int min_int ```

Basic Sequence Functions
Basic Operations Description Work Depth
`#a` Length of `a` O(1) O(1)
`a[i]` `i`th element of `a` O(1) O(1)
`dist(a,n)` Create sequence of length `n` with `a` in each element. O(n) O(1)
`zip(a,b)` Elementwise zip two sequences together into a sequence of pairs. O(n) O(1)
`[s:e]` Create sequence of integers from `s` to `e` (not inclusive of `e`) O(e-s) O(1)
`[s:e:d]` Same as `[s:e]` but with a stride `d`. O((e-s)/d) O(1)
Scans
`plus_scan(a)` Execute a scan on `a` using the `+` operator O(n) O(log n)
`min_scan(a)` Execute a scan on `a` using the `minimum` operator O(n) O(log n)
`max_scan(a)` Execute a scan on `a` using the `maximum` operator O(n) O(log n)
`or_scan(a)` Execute a scan on `a` using the `or` operator O(n) O(log n)
`and_scan(a)` Execute a scan on `a` using the `and` operator O(n) O(log n)
Reductions
`sum(a)` Sum the elements of `a` O(n) O(log n)
`max_val(a)` Return maximum value of `a` O(n) O(log n)
`min_val(a)` Return minimum value of `a` O(n) O(log n)
`any(a)` Return true if any values of `a` are true. O(n) O(log n)
`all(a)` Return true only if all values of `a` are true. O(n) O(log n)
`count(a)` Count number of true values in `a`. O(n) O(log n)
`max_index(a)` Return position (index) of maximum value. O(n) O(log n)
`min_index(a)` Return position (index) of minimum value. O(n) O(log n)
Reordering Functions
`read(a, i)` Read values in `a` from indices `i` O(n) O(1)
`write(a, iv_pairs)` Write values in `a` using integer values pairs in `iv_pairs` O(#iv_pairs)* O(1)
`permute(a,i)` Permute elements in `a` to indices in `i` O(n) O(1)
`rotate(a,i)` Rotate sequence `a` by `i` locations O(n) O(1)
`reverse(a)` Reverse order of sequence `a`. O(n) O(1)
`drop(a,i)` Drop first `i` elements of `a`. O(#a-i) O(1)
`take(a,i)` Take first `i` elements of `a`. O(i) O(1)
`odd_elts(a)` Return the odd elements of `a` O(n) O(1)
`even_elts(a)` Return the even elements of `a` O(n) O(1)
`interleave(a,b)` Interleave the elements of `a` and `b`. O(n) O(1)
`subseq(a,i,j)` Return the subsequence of `a` from position `i` to `j` (not inclusive of `j`) O(j-i) O(1)
`a -> i` Same as `read(a, i)` O(#i) O(1)
`a <- iv_pairs` Same as `write(a, iv_pairs)` O(#iv_pairs)* O(1)
Nesting/Unnesting
`flatten(a)` Flatten a nested sequence by one level. O(#a) O(1)
`partition(a,l)` Partition a sequence into a nested sequence using lengths in `l` O(#a) O(1)
`bottop(a)` Split a sequence in two and return as nested sequence. O(n) O(1)
* Note that `write(a, iv_pairs)` (and ```a <- iv_pairs```) take O(#iv_pairs) time if the destination is the only copy that is referenced, otherwise it takes time O(#a).

### Other functions

See the the language definition for the documentation on the functions listed below, or type the function name after `describe` in the window that appears with each group and submit (make sure the line ends with a semicolon).

#### Other sequence functions

These are more complex sequence functions. The depth complexities of these functions are not necessarily O(1).
```  length_from_flags

sort rank collect int_collect kth_smallest find
search_for_subseqs remove_duplicates mark_duplicates
union intersection name
transpose

eql hash select identity
```

#### Operations on strings

```  @ exp_string ||
linify wordify
lowercase uppercase string_eql
parse_int parse_float
```

#### Input and Output

Of the functions listed in this section, only `print_char`, `print_string`, `write_char`, `write_string`, and `write_check` can be called in parallel.
```  print_char print_string
write_object_to_file  write_string_to_file
append_string_to_file
read_object_from_file read_string_from_file
read_int_seq_from_file read_float_seq_from_file
open_in_file open_out_file close_file
write_char write_string
read_char read_string read_line read_word
open_check write_check read_check close_check
nullstr stdin stdout stderr
```

#### Window functions

The functions in this section can be used for plotting data on an Xwindow display. The basic idea of these functions is that you create a window with the `w_make_window` command and then can add various features to the window. The most important features are boxes and buttons. A scale box can be used to create a virtual coordinate system on the window on which points, lines, rectangles and polygons can be drawn. A text box can be used to create a box in which text can be written. A button can be used along with the input functions to get information back from the window. In all the functions in this section colors are specified by one of
```w_black, w_white, w_red, w_blue, w_green, w_cyan, w_yellow, w_magenta, w_pink, w_light_green, w_light_blue, w_purple, w_gray, w_dark_gray, w_orange. ```
```  display

w_make_window w_kill_window
w_add_box w_add_text_box w_add_button w_add_button_stack
w_add_text

w_get_named_box w_reset_box_size w_clear_box
w_bounds_from_box w_box_scale w_bounding_box

w_draw_point w_draw_big_point w_draw_points
w_draw_segments w_draw_string w_draw_rectangle
w_shade_rectangle w_shade_polygon

w_write_text_centered w_write_text_left w_write_paragraph
w_get_input w_get_input_noblock w_get_button_input w_get_zoom_box
```

#### System calls

The functions in this section can be used to execute shell commands from within NESL.
```  shell_command get_environment_variable spawn

time
```

back to the NESL page

Guy Blelloch, blelloch@cs.cmu.edu.