Cat-Language.com |
InstructionsDefinitionsdefine - : ('A 'b 'c -> 'A any) {{ desc: A symbolic form of sub. tags: level1,math }} { sub } define != : ('A 'b 'b -> 'A bool) {{ desc: A symbolic form of neq (not equals). tags: level1,math }} { neq } define #clr : ('A -> ) {{ desc: removes all items from the stack tags: meta }} { _primitive_ } define #def : (string -> ) {{ desc: outputs a detailed description of the instruction tags: meta }} { _primitive_ } define #defmatch : (string -> ) {{ desc: outputs a detailed description of all instructions starting with the name tags: meta }} { _primitive_ } define #defs : ( -> ) {{ desc: outputs a complete list of defined instructions tags: meta }} { _primitive_ } define #deftag : (string -> ) {{ desc: outputs a list of the functions associated with a tag }} { _primitive_ } define #deftags : ( -> ) {{ desc: outputs a list of the different tags associated with the functions }} { _primitive_ } define #h : (('A -> 'B) -> ('A -> 'B) list) {{ }} { #metadata } define #help : ( -> ) {{ desc: outputs a help message tags: meta }} { _primitive_ } define #i : (('A -> 'B) -> ('A -> 'B)) {{ }} { #inline } define #i0 : (('A -> 'B) -> ('A -> 'B)) {{ }} { #inline-step } define #inline : (('A -> 'B) -> ('A -> 'B)) {{ desc: performs inline expansion tags: meta }} { _primitive_ } define #inline-step : (('A -> 'B) -> ('A -> 'B)) {{ desc: performs inline expansion tags: meta }} { _primitive_ } define #load : (string -> ) {{ desc: loads and executes a source code file tags: meta }} { _primitive_ } define #m : (('A -> 'B) -> ('A -> 'B)) {{ }} { #metacat } define #make_help : ( -> ) {{ desc: generates an HTML help file tags: meta }} { _primitive_ } define #metacat : (('A -> 'B) -> ('A -> 'B)) {{ desc: runs MetaCat rewriting rules tags: meta }} { _primitive_ } define #metadata : (('A -> 'B) -> ('A -> 'B) list) {{ desc: outputs the meta-data associated with a function tags: meta }} { _primitive_ } define #save : (string -> ) {{ desc: saves a transcript of the session so far tags: meta }} { _primitive_ } define #t : (function -> function) {{ }} { #type } define #test : (('A -> 'B) -> ('A -> 'B)) {{ desc: runs the unit tests associated with an instruction tags: meta }} { _primitive_ } define #test_all : ( -> ) {{ desc: runs all unit tests associated with all instructions tags: meta }} { _primitive_ } define #trace : ('A ('A -> 'B) -> 'B) {{ desc: used to trace the execution of a function tags: meta }} { _primitive_ } define #type : (function -> function) {{ desc: displays the type of an expression tags: meta }} { _primitive_ } define % : ('A 'b 'c -> 'A any) {{ desc: A symbolic form of mod. tags: level1,math }} { mod } define * : ('A 'b 'c -> 'A any) {{ desc: A symbolic form of mul. tags: level1,math }} { mul } define / : ('A 'b 'c -> 'A any) {{ desc: A symbolic form of div. tags: level1,math }} { div } define + : ('A 'b 'c -> 'A any) {{ desc: A symbolic form of add. tags: level1,math }} { add } define < : ('A 'b 'c -> 'A bool) {{ desc: A symbolic form of lt. tags: level1,math }} { lt } define <= : ('A 'b 'c -> 'A bool) {{ desc: A symbolic form of lteq. tags: level1,math }} { lteq } define == : ('a 'a -> bool) {{ desc: A symoblic form of eq tags: level1,math }} { eq } define > : ('A 'b 'c -> 'A bool) {{ desc: A symbolic form of gt. tags: level1,math }} { gt } define >= : ('A 'b 'c -> 'A bool) {{ desc: A symbolic form of gteq. tags: level1,math }} { gteq } define A : ('a ('a -> 'b) -> 'b) {{ desc: applies a unary function to its argument tags: functions }} { _primitive_ } define abs_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define acos : ('A double -> 'A double) {{ }} { acos_dbl } define acos_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define add : ('A 'b 'c -> 'A any) {{ desc: Adds top value to second value. tags: level1,math }} { [[add_int] int_type [add_dbl] double_type [add_byte] byte_type [add_bit] bit_ typ e [add_str] string_type] list dispatch2 } define add_bit : ('A Bit Bit -> 'A Bit) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define add_byte : ('A Byte Byte -> 'A Byte) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define add_dbl : ('A double double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define add_int : (int int -> int) {{ desc: adds two integers tags: level0,math }} { _primitive_ } define add_str : ('A string string -> 'A string) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define add_time : (date_time time_span -> date_time) {{ desc: computes a date by adding a time period to a date tags: level2,datetime }} { _primitive_ } define and : (bool bool -> bool) {{ desc: returns true if both of the top two values on the stack are true tags: level0,boolean }} { _primitive_ } define app2 : ('A 'b 'b ('A 'b -> 'A) -> 'A) {{ desc: Applies a function to the stack twice, first with the top value removed then next with the second value removed. test: in: 7 2 3 [sub_int] app2 out: 2 tags: level1,functions }} { dup swapd [apply] dip2 apply } define apply : ('A ('A -> 'B) -> 'B) {{ desc: applies a function to the stack (i.e. executes an instruction) tags: level0,functions }} { _primitive_ } define apply2 : ('A ('A -> 'A) -> 'A) {{ desc: Applies a function to a stack twice. semantics: $A [$B] apply2 == $A $B $B test: in: 40 [1 add_int] apply2 out: 42 tags: level1,functions }} { keep apply } define are_eq : ('A 'b 'b -> 'A 'b 'b bool) {{ }} { dup2 eq } define are_gt : ('A 'b 'c -> 'A 'b 'c bool) {{ }} { dup2 gt } define are_gteq : ('A 'b 'c -> 'A 'b 'c bool) {{ }} { dup2 gteq } define are_lt : ('A 'b 'c -> 'A 'b 'c bool) {{ }} { dup2 lt } define are_lteq : ('A 'b 'c -> 'A 'b 'c bool) {{ }} { dup2 lteq } define are_neq : ('A 'b 'b -> 'A 'b 'b bool) {{ }} { dup2 neq } define as_bool : (any -> bool) {{ desc: casts a variant to a bool tags: level1,conversion }} { _primitive_ } define as_char : (any -> char) {{ desc: casts a variant to a double tags: level1,conversion }} { _primitive_ } define as_dbl : (any -> double) {{ desc: casts a variant to a double tags: level1,conversion }} { _primitive_ } define as_int : (any -> int) {{ desc: casts a variant to an int tags: level1,conversion }} { _primitive_ } define as_list : (any -> list) {{ desc: casts a variant to a list tags: level1,conversion }} { _primitive_ } define as_string : (any -> string) {{ desc: casts a variant to a char tags: level1,conversion }} { _primitive_ } define as_var : ('a -> var) {{ desc: casts anything into a variant tags: level1,conversion }} { _primitive_ } define asin : ('A double -> 'A double) {{ }} { asin_dbl } define asin_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define assert : ('A bool -> 'A) {{ desc: Used to check that preconditions or postconditions are true tags: level1,misc }} { assertion failed throw_if } define atan : ('A double -> 'A double) {{ }} { atan_dbl } define atan_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define atan2 : ('A double -> 'A double) {{ }} { atan_dbl } define atan2_dbl : ('A double double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define back : ('A 'b -> 'A) {{ }} { bk } define bin_rec : ('a ('a -> bool) ('a -> 'b) ('a -> 'C 'a 'a) ('C 'b 'b -> 'b) -> 'b) {{ desc: execute a binary recursion process tags: level1,control }} { _primitive_ } define bin_str : (int -> string) {{ desc: converts a number into a binary string representation. tags: level2,strings,math,conversion }} { _primitive_ } define bit_type : ( -> type) {{ desc: pushes a value representing the type of a bit tags: level1,types }} { _primitive_ } define bk : ('A 'b -> 'A) {{ }} { neg fd } define blue : ('A -> 'A) {{ }} { 0 0 255 rgb } define bool_type : ( -> type) {{ desc: pushes a value representing the type of a boolean tags: level1,types }} { _primitive_ } define bury : ('A 'b 'c 'd -> 'A 'd 'b 'c) {{ desc: Place the top item on the stack under the third item. test: in: 1 2 3 bury out: 3 1 2 tags: level1,stack }} { swap swapd } define bury4 : ('A 'b 'c 'd 'e -> 'A 'e 'b 'c 'd) {{ desc: Moves the top value to under the fourth value. test: in: 1 2 3 4 bury4 out: 4 1 2 3 tags: level1,stack }} { swap [bury] dip } define bury5 : ('A 'b 'c 'd 'e 'f -> 'A 'f 'b 'c 'd 'e) {{ desc: Moves the top value to under the fifth value. test: in: 1 2 3 4 5 bury5 out: 5 1 2 3 4 tags: level1,stack }} { swap [bury4] dip } define byte_type : ( -> type) {{ desc: pushes a value representing the type of a byte tags: level1,types }} { _primitive_ } define case : ('A int -> 'B) {{ desc: used to construct a 'case' statement member of a switch statement tags: level1,control,functions }} { _primitive_ } define cat : ('A list list -> 'A list) {{ desc: Concatenates two lists together test: in: [1 2] list [3 4] list cat out: [1 2 3 4] list tags: level1,lists }} { swap [cons] rfold } define ceil : ('A double -> 'A double) {{ }} { ceil_dbl } define ceil_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define char_to_int : ('A char -> 'A int) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define char_to_str : ('A char -> 'A string) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define clear : ( -> ) {{ desc: clears the drawing window tags: level2,graphics }} { _primitive_ } define close_stream : (stream -> ) {{ desc: closes a stream tags: level2,streams }} { _primitive_ } define close_window : ( -> ) {{ desc: close a drawing window tags: level2,graphics }} { _primitive_ } define clr_enumerable_to_list : (any -> list) {{ desc: converts a .NET IEnumerable object into a list tags: clr }} { _primitive_ } define clr_get_field : (string any -> any any) {{ desc: retrieves the value of a field from a .NET object tags: clr }} { _primitive_ } define clr_invoke : (list string any -> any any) {{ desc: calls a method on a .NET object tags: clr }} { _primitive_ } define clr_list_fields : (any -> list any) {{ desc: retrieves a list of field names from a .NET object tags: clr }} { _primitive_ } define clr_list_methods : (any -> list any) {{ desc: retrieves a list of field names from a .NET object tags: clr }} { _primitive_ } define clr_new : (list string -> any) {{ desc: constructs a new .NET object tags: clr }} { _primitive_ } define clr_set_field : (any string any -> any) {{ desc: assigns a value to a field of a .NET object tags: clr }} { _primitive_ } define compare : ('A 'b 'c -> 'A 'b 'c bool) {{ }} { compare_lteq } define compare_gt : ('A 'b 'c -> 'A 'b 'c bool) {{ }} { dup2 gt } define compare_gteq : ('A 'b 'c -> 'A 'b 'c bool) {{ }} { dup2 lt } define compare_lt : ('A 'b 'c -> 'A 'b 'c bool) {{ }} { dup2 lt } define compare_lteq : ('A 'b 'c -> 'A 'b 'c bool) {{ }} { dup2 lteq } define compl_bit : ('A Bit -> 'A Bit) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define compl_byte : ('A Byte -> 'A Byte) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define compl_int : (int -> int) {{ desc: performs a bitwise complement of the top value tags: level0,math }} { _primitive_ } define compose : (('A -> 'B) ('B -> 'C) -> ('A -> 'C)) {{ desc: creates a function by composing (concatenating) two existing functions tags: level0,functions }} { _primitive_ } define cons : (list 'a -> list) {{ desc: prepends an item to a list tags: level0,lists }} { _primitive_ } define cons_first : ('A list 'b -> 'A list) {{ desc: Appends item to first list in a pair of lists test: in: [1 2] list [3 4] list pair 5 cons_first out: [1 2] list [3 4 5] list pair tags: level2,lists }} { [unpair] dip cons pair } define cons_second : ('A list 'b -> 'A list) {{ test: in: [1 2] list [3 4] list pair 5 cons_second out: [1 2 5] list [3 4] list pair tags: level2,lists }} { [unpair] dip swap [cons] dip pair } define consd : ('A list 'b 'c -> 'A list 'c) {{ desc: Append the second item on stack to a list in the third position. test: in: [1 2] list 3 4 consd out: [1 2 3] list 4 tags: level1,lists }} { [cons] dip } define cos : ('A double -> 'A double) {{ }} { cos_dbl } define cos_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define cosh : ('A double -> 'A double) {{ }} { cosh_dbl } define cosh_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define count : (list -> list int) {{ desc: returns the number of items in a list tags: level1,lists }} { _primitive_ } define cw : ( -> ) {{ }} { close_window } define dec : ('A int -> 'A int) {{ desc: Subtract one from an integer on the stack test: in: 5 dec out: 4 tags: level0,math }} { 1 sub_int } define dec_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define default : ('A -> 'B) {{ desc: used to construct a default 'case' statement tags: level1,control,functions }} { _primitive_ } define demo : ('A -> 'A) {{ }} { demo2 } define demo1 : ('A -> 'A) {{ desc: Draws a simple fractal }} { 90 tl 80 fractal1 } define demo2 : ('A -> 'A) {{ desc: Draws the demo1 fractal and its mirror image }} { [90 tl demo1] 2 repeat } define dig : ('A 'b 'c 'd -> 'A 'c 'd 'b) {{ desc: Places the third item on the stack on the top. test: in: 1 2 3 dig out: 2 3 1 tags: level1,stack }} { swapd swap } define dig4 : ('A 'b 'c 'd 'e -> 'A 'c 'd 'e 'b) {{ desc: Moves the fourth value to the top. test: in: 1 2 3 4 dig4 out: 2 3 4 1 tags: level1,stack }} { [dig] dip swap } define dig5 : ('A 'b 'c 'd 'e 'f -> 'A 'c 'd 'e 'f 'b) {{ desc: Moves the fifth value to the top. test: in: 1 2 3 4 5 dig5 out: 2 3 4 5 1 tags: level1,stack }} { [dig4] dip swap } define dip : ('A 'b ('A -> 'C) -> 'C 'b) {{ desc: evaluates a function, temporarily removing second item tags: level0,functions }} { _primitive_ } define dip2 : ('A 'b 'c ('A -> 'D) -> 'D 'b 'c) {{ desc: Applies a function to the stack, temporarily removing the next two items. test: in: 1 2 3 4 [add_int] dip2 out: 3 3 4 tags: level1,functions }} { swap [dip] dip } define dip3 : ('A 'b 'c 'd ('A -> 'E) -> 'E 'b 'c 'd) {{ desc: Applies a function to the stack, temporarily removing the next three items. test: in: 1 2 3 4 5 [add_int] dip3 out: 3 3 4 5 tags: level1,functions }} { swap [dip2] dip } define dip4 : ('A 'b 'c 'd 'e ('A -> 'F) -> 'F 'b 'c 'd 'e) {{ desc: Applies a function to the stack, temporarily removing the next four items. test: in: 1 2 3 4 5 6 [add_int] dip4 out: 3 3 4 5 6 tags: level1,functions }} { swap [dip3] dip } define dispatch1 : ('a list -> any) {{ desc: dynamically dispatches a function depending on the type on top of the stack tags: level1,functions }} { _primitive_ } define dispatch2 : ('a 'b list -> any) {{ desc: dynamically dispatches a function depending on the type on top of the stack tags: level1,functions }} { _primitive_ } define div : ('A 'b 'c -> 'A any) {{ desc: Divides the top value from the second value. tags: level1,math }} { [[div_int] int_type [div_dbl] double_type [div_byte] byte_type [div_bit] bit_ typ e] list dispatch2 } define div_bit : ('A Bit Bit -> 'A Bit) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define div_byte : ('A Byte Byte -> 'A Byte) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define div_dbl : ('A double double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define div_int : (int int -> int) {{ desc: divides the top value (an integer) from the second value (an integer) tags: level0,math }} { _primitive_ } define double_type : ( -> type) {{ desc: pushes a value representing the type of a double tags: level1,types }} { _primitive_ } define draw_decagon : ('A 'b -> 'A) {{ }} { id 10 draw_reg_poly } define draw_dodecagon : ('A 'b -> 'A) {{ }} { id 12 draw_reg_poly } define draw_hendecagon : ('A 'b -> 'A) {{ }} { id 11 draw_reg_poly } define draw_hexagon : ('A 'b -> 'A) {{ }} { id 6 draw_reg_poly } define draw_hexagram : ('A list double int -> 'A list) {{ }} { id 6 draw_polygram } define draw_nonagon : ('A 'b -> 'A) {{ }} { id 9 draw_reg_poly } define draw_octagon : ('A 'b -> 'A) {{ }} { id 8 draw_reg_poly } define draw_pentagon : ('A 'b -> 'A) {{ }} { id 5 draw_reg_poly } define draw_pentagram : ('A list double int -> 'A list) {{ }} { id 5 draw_polygram } define draw_polygram : ('A list double int 'b -> 'A list) {{ desc: Draws a regular star polygon }} { [[[[id] papply dip id] dip id + point_on_circle] papply papply] papply dip id for } define draw_reg_line_seq : ('A 'b int 'c -> 'A) {{ desc: Draws a sequence of "n" lines with length "len" and an angle of "angle". }} { [[[id fd] dip id tr] papply papply] papply dip id repeat } define draw_reg_poly : ('A 'b int -> 'A) {{ desc: Draws a regular polygon with each side of length "side" and with "n" sides }} { dup [[id] dip id 360] dip id / draw_reg_line_seq } define draw_septagon : ('A 'b -> 'A) {{ }} { id 7 draw_reg_poly } define draw_square : ('A 'b -> 'A) {{ }} { id 4 draw_reg_poly } define draw_triangle : ('A 'b -> 'A) {{ }} { id 3 draw_reg_poly } define dup : ('a -> 'a 'a) {{ desc: duplicate the top item on the stack tags: level0,stack }} { _primitive_ } define dup2 : ('A 'b 'c -> 'A 'b 'c 'b 'c) {{ desc: Duplicates the top two values. test: in: 1 2 dup2 out: 1 2 1 2 tags: level1,stack }} { get2 get2 } define dup3 : ('A 'b 'c 'd -> 'A 'b 'c 'd 'b 'c 'd) {{ desc: Duplicates the top three values. test: in: 1 2 3 dup3 out: 1 2 3 1 2 3 tags: level1,stack }} { get3 get3 get3 } define dup4 : ('A 'b 'c 'd 'e -> 'A 'b 'c 'd 'e 'b 'c 'd 'e) {{ desc: Duplicates the top four values test: in: 1 2 3 4 dup4 out: 1 2 3 4 1 2 3 4 tags: level1,stack }} { get4 get4 get4 get4 } define dupd : ('A 'b 'c -> 'A 'b 'b 'c) {{ desc: Duplicate the second item on the stack. test: in: 1 2 dupd out: 1 1 2 semantics: $a $b swapd == $a $a $b tags: level0,stack }} { [dup] dip } define dupdd : ('A 'b 'c 'd -> 'A 'b 'b 'c 'd) {{ desc: Duplicate the third item on the stack. test: in: 1 2 3 dupdd out: 1 1 2 3 semantics: $a $b $c dupdd == $a $a $b $c tags: level0,stack }} { [dupd] dip } define e : ('A -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define empty : (list -> list bool) {{ desc: returns true if the list is empty tags: level0,lists }} { _primitive_ } define eq : ('a 'a -> bool) {{ desc: returns true if both items on stack have the same value tags: level1,comparison }} { _primitive_ } define eqf : ('A 'b -> 'A ('C 'b -> 'C bool)) {{ }} { [eq] papply } define eqz : ('A int -> 'A bool) {{ desc: Returns true if the top value is zero test: in: 5 eqz out: false test: in: 0 eqz out: true tags: level0,math }} { 0 eq } define eval : (list string -> list) {{ desc: evaluates a string as a function using the list as a stack tags: level2,misc }} { _primitive_ } define even : ('A int -> 'A bool) {{ tags: level1,boolean }} { 2 modn } define explode : ('A -> 'B) {{ desc: breaks a function up into a list of instructions tags: level2,functions }} { _primitive_ } define fact : ('A int -> 'A int) {{ desc: A simple factorial function precondition: dup 0 gteq test: in: 5 fact out: 120 tags: demo,algorithms }} { dup eqz [pop 1] [dup dec fact mul_int] if } define false : ( -> bool) {{ desc: pushes the boolean value false on the stack tags: level0,boolean }} { _primitive_ } define fd : ('A 'b -> 'A) {{ }} { dup 0 line_to 0 move_to } define fib : ('A int -> 'A int) {{ desc: A simple implementation of the fibonacci function test: in: 0 fib out: 1 test: in: 1 fib out: 1 test: in: 5 fib out: 8 tags: demo,algorithms }} { [dup 2 lt_int] [pop 1] [dec dup dec] [add_int] bin_rec } define file_exists : (string -> string bool) {{ desc: returns a boolean value indicating whether a file or directory exists tags: level2,streams }} { _primitive_ } define file_reader : (string -> istream) {{ desc: creates an input stream from a file name tags: level2,streams }} { _primitive_ } define file_writer : (string -> ostream) {{ desc: creates an output stream from a file name tags: level2,streams }} { _primitive_ } define filter : ('A list ('A list any any -> 'B list 'c bool) -> 'A list) {{ desc: Creates a list from another keeping only values for which the predicate is t rue semantics: define filter(xs pred) { xs nil [dup pred [cons] [pop] if] rfold } test: in: [1 2 3 4] list [even] filter out: [2 4] list tags: level1,lists }} { [dupd apply [cons] [pop] if] papply nil swap rfold } define first : ('A list -> 'A list any) {{ desc: Returns the first item in a list test: in: [1 2 3] list first out: [1 2 3] list 3 tags: level1,lists }} { 0 get_at } define flatten : ('A list -> 'A list) {{ desc: Converts a list of lists into a single list, by appending them together test: in: [[1 2] list [3] list [] list] list flatten out: [1 2 3] list tags: level1,lists }} { nil [cat] rfold } define flip : ('A 'b 'c 'd -> 'A 'd 'c 'b) {{ desc: Swaps the top and third item on the stack. test: in: 1 2 3 flip out: 3 2 1 tags: level1,stack }} { swapd swap swapd } define flip4 : ('A 'b 'c 'd 'e -> 'A 'e 'c 'd 'b) {{ desc: Swaps the top and fourth item on the stack. test: in: 1 2 3 4 flip4 out: 4 2 3 1 tags: level1,stack }} { swap [flip] dip swap } define flip5 : ('A 'b 'c 'd 'e 'f -> 'A 'f 'c 'd 'e 'b) {{ desc: Swaps the top and fifth item on the stack. test: in: 1 2 3 4 5 flip5 out: 5 2 3 4 1 tags: level1,stack }} { swap [flip4] dip swap } define floor : ('A double -> 'A double) {{ }} { floor_dbl } define floor_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define fold : ('A list 'b ('A 'b any -> 'C 'd) -> 'A 'b) {{ desc: Also known as a reduce function, this combines adjacent values in a list usi ng an initial value and a binary function. semantics: define fold(xs x f) { xs empty [x] [xs uncons x swap f apply f fold] if } test: in: [1 2 3 4] list 0 [add_int] fold out: 10 test: in: [1 2 3 4] list 0 [popd] fold out: 1 tags: level0,lists }} { dig empty [pop2] [uncons swap [swap keep] dip bury fold] if } define foldwlist : ('A list list list 'b ('A list list 'b any -> 'A list list list 'b) -> 'A list list 'b) {{ desc: Like fold except the list being folded is an argument to the function. tags: level1,lists }} { [empty] dip2 dig [pop popd] [[[uncons] dip swap] dip keep fold] if } define for : ('A ('A int -> 'A) int -> 'A) {{ desc: A for loop. Behaves like repeat but an index value is passed to the loop bod y on each iteration, starting at 0. test: in: 0 [add_int] 5 for out: 10 tags: level2,control }} { 0 bury swap [dupd swap [apply] dip inc] papply swap [dupd neq] papply while p op } define foreach : ('A list ('A any -> 'A) -> 'A) {{ desc: Executes a function with each item in the list, and consumes the list. semantics: $A $b [$C] foreach == $A $b empty not [uncons pop [$C] foreach] [pop] if } test: in: 0 [1 2 3 4] list [add_int] foreach out: 10 tags: level2,control }} { [dip] papply [uncons swap] rcompose whilene } define format_currency : ('A double -> 'A string) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define format_scientific : ('A double -> 'A string) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define forward : ('A 'b -> 'A) {{ }} { fd } define frac : ('A int int int -> 'A int) {{ desc: Multiplies an integer by a fraction. x num den -> (x * num) / den) test: in: 6 2 3 frac out: 4 semantics: $a $b $c frac == $a $b mul_int $c div_int tags: level1,math }} { [mul_int] dip div_int } define fractal0 : ('A int -> 'A) {{ }} { dup sq 15 tr pu dup fd pd dup 5 < [pop] [3 4 frac fractal0] if } define fractal1 : ('A int -> 'A) {{ }} { dup sq dup 3 < [pop] [dup fd dup 4 5 frac 45 tl fractal1 45 tr bk] if } define get_adjacent : ('A 'b double -> 'A any) {{ desc: Given the hypotenuse of a triangle and the angle theta computes the length o f the adjacent side semantics: get_adjacent(hyp theta) => acos(theta) * hyp }} { acos * } define get_at : (list int -> list any) {{ desc: returns the nth item in a list tags: level1,lists }} { _primitive_ } define get_opposite : ('A 'b double -> 'A any) {{ desc: Given the hypotenuse of a triangle and the angle theta computes the length o f the opposite side semantics: get_opposite(hyp theta) => asin(theta) * hyp }} { asin * } define get2 : ('A 'b 'c -> 'A 'b 'c 'b) {{ desc: Pushes a copy of the value from the second location on the stack to the top. test: in: 1 2 get2 out: 1 2 1 tags: level1,stack }} { [dup] dip swap } define get3 : ('A 'b 'c 'd -> 'A 'b 'c 'd 'b) {{ desc: Pushes a copy of the value from the third location on the stack to the top. test: in: 1 2 3 get3 out: 1 2 3 1 tags: level1,stack }} { [get2] dip swap } define get4 : ('A 'b 'c 'd 'e -> 'A 'b 'c 'd 'e 'b) {{ desc: Pushes a copy of the value from the fourth location on the stack to the top. test: in: 1 2 3 4 get4 out: 1 2 3 4 1 tags: level1,stack }} { [get3] dip swap } define getval : (ref -> ref any) {{ desc: gets value associated with reference tags: level2,ref }} { _primitive_ } define green : ('A -> 'A) {{ }} { 0 255 0 rgb } define gt : ('A 'b 'c -> 'A bool) {{ desc: Pushes true if the top value is greater than the second value, false otherwi se. tags: level1,math }} { [[gt_int] int_type [gt_dbl] double_type [gt_byte] byte_type [gt_bit] bit_type [g t_str] string_type] list dispatch2 as_bool } define gt_bit : ('A Bit Bit -> 'A bool) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define gt_byte : ('A Byte Byte -> 'A bool) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define gt_dbl : ('A double double -> 'A bool) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define gt_int : (int int -> bool) {{ desc: replaces the top two values (integers) with true, if the second value is gre ater than the top value, otherwise pushes false tags: level0,math }} { _primitive_ } define gt_str : ('A string string -> 'A bool) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define gteq : ('A 'b 'c -> 'A bool) {{ desc: Pushes true if the top value is greater than or equal to the second value, f alse otherwise. tags: level1,math }} { [[gteq_int] int_type [gteq_dbl] double_type [gteq_byte] byte_type [gteq_bit] bit _type [gteq_str] string_type] list dispatch2 as_bool } define gteq_bit : ('A Bit Bit -> 'A bool) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define gteq_byte : ('A Byte Byte -> 'A bool) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define gteq_dbl : ('A double double -> 'A bool) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define gteq_int : (int int -> bool) {{ desc: replaces the top two values (integers) with true, if the second value is gre ater than or equal to the top value, otherwise pushes false tags: level0,math }} { _primitive_ } define gteq_str : ('A string string -> 'A bool) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define gteqf : ('A 'b -> 'A ('C 'd -> 'C bool)) {{ }} { [gteq] papply } define gteqz : ('A int -> 'A bool) {{ tags: level1,boolean }} { 0 gteq_int } define gtf : ('A 'b -> 'A ('C 'd -> 'C bool)) {{ }} { [gt] papply } define gtz : ('A int -> 'A bool) {{ tags: level1,boolean }} { 0 gt_int } define halt : (int -> ) {{ desc: halts the program with an error code tags: level2,application }} { _primitive_ } define hash_add : (hash_list any any -> hash_list) {{ desc: associates the second value with a key (the top value) in a hash list tags: level2,hash }} { _primitive_ } define hash_add_chain : ('A hash_list 'b any -> 'A hash_list) {{ desc: Adds an element as unit list to a hash_list if it doesn't exist, otherwise c hain it to the current list item. test: in: hash_list 1 "a" hash_add_chain "a" hash_get popd out: [1] list test: in: hash_list 1 "a" hash_add_chain 2 "a" hash_add_chain "a" hash_get popd out: [1 2] list tags: level2,hash }} { dup swapd [[nil hash_safe_get] dip cons] dip hash_set } define hash_contains : (hash_list any -> hash_list bool) {{ desc: returns true if hash list contains key tags: level2,hash }} { _primitive_ } define hash_get : (hash_list any -> hash_list any) {{ desc: gets a value from a hash list using a key tags: level2,hash }} { _primitive_ } define hash_list : ( -> hash_list) {{ desc: makes an empty hash list tags: level2,hash }} { _primitive_ } define hash_safe_get : ('A hash_list any any -> 'A hash_list any) {{ desc: Gets a value from a hash_list. If it doesn't exist then uses the default test: in: hash_list 1 "a" hash_set "a" 0 hash_safe_get popd out: 1 test: in: hash_list 1 "a" hash_set "b" 0 hash_safe_get popd out: 0 tags: level2,hash }} { [dup [hash_contains] dip] dip [[hash_get] papply] dip quote if } define hash_set : (hash_list any any -> hash_list) {{ desc: associates the second value with a key (the top value) in a hash list tags: level2,hash }} { _primitive_ } define hash_to_list : (hash_list -> list) {{ desc: converts a hash_list to a list of pairs tags: level2,hash }} { _primitive_ } define head : ('A list -> 'A any) {{ desc: Replaces a list with the first item test: in: [1 2 3] list head out: 3 tags: level1,lists }} { uncons popd } define head_eq : ('A list any -> 'A list bool) {{ desc: Returns true if the head of a list is equal to the value on the top of the s tack. test: in: [1 2] list 2 head_eq popd out: true test: in: [1 2] list 1 head_eq popd out: false tags: level1,lists }} { [first] dip eq } define hex_str : (int -> string) {{ desc: converts a number into a hexadecimal string representation. tags: level2,strings,math,conversion }} { _primitive_ } define hide_turtle : ('A -> 'A) {{ }} { false set_turtle_visibility } define ht : ('A -> 'A) {{ }} { hide_turtle } define id : ('a -> 'a) {{ desc: does nothing, but requires one item on the stack. tags: level1,misc }} { _primitive_ } define if : ('A bool ('A -> 'B) ('A -> 'B) -> 'B) {{ desc: executes one predicate or another whether the condition is true tags: level0,control }} { _primitive_ } define if_pred : ('A ('B -> 'B) ('A -> 'B bool) -> 'B) {{ desc: Executes a function only if the predicate is true. }} { dip [] if } define ifeq : ('A 'b ('A 'b -> 'A 'b) 'b -> 'A 'b) {{ desc: Executes a function if the value below it is equal to a given value. test: in: 3 1 [[inc] dip] 1 ifeq out: 4 1 test: in: 3 2 [[inc] dip] 1 ifeq out: 3 2 }} { is_eqf if_pred } define ifeqz : ('A int ('A int -> 'A int) -> 'A int) {{ desc: Executes a function if the value below it is zero test: in: 3 1 [[inc] dip] ifeqz out: 3 1 test: in: 3 0 [[inc] dip] ifeqz out: 4 0 }} { 0 ifeq } define ifneq : ('A 'b ('A 'b -> 'A 'b) 'b -> 'A 'b) {{ desc: The negation of ifeq }} { is_neqf if_pred } define ifneqz : ('A int ('A int -> 'A int) -> 'A int) {{ desc: Executes a function if the value below it is not zero }} { 0 ifneq } define inc : ('A int -> 'A int) {{ desc: Adds one to an integer on the stack test: in: 5 inc out: 6 tags: level0,math }} { 1 add_int } define inc_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define index_of : ('A string string -> 'A int) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define int_to_byte : (int -> byte) {{ desc: converts an integer into a byte, throwing away sign and ignoring higher bits tags: level1,math,conversion }} { _primitive_ } define int_to_char : ('A int -> 'A char) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define int_type : ( -> type) {{ desc: pushes a value representing the type of an int tags: level1,types }} { _primitive_ } define is_eq : ('A 'b 'b -> 'A 'b bool) {{ tags: level1,boolean }} { dupd eq } define is_eqf : ('A 'b -> 'A ('C 'b -> 'C 'b bool)) {{ }} { [is_eq] papply } define is_eqz : ('A int -> 'A int bool) {{ tags: level1,boolean }} { dup 0 eq } define is_gt : ('A 'b 'c -> 'A 'b bool) {{ tags: level1,boolean }} { dupd gt } define is_gteq : ('A 'b 'c -> 'A 'b bool) {{ tags: level1,boolean }} { dupd gteq } define is_gteqf : ('A 'b -> 'A ('C 'd -> 'C 'd bool)) {{ }} { [is_gteq] papply } define is_gteqz : ('A 'b -> 'A 'b bool) {{ tags: level1,boolean }} { dup 0 gteq } define is_gtf : ('A 'b -> 'A ('C 'd -> 'C 'd bool)) {{ }} { [is_gt] papply } define is_gtz : ('A 'b -> 'A 'b bool) {{ tags: level1,boolean }} { dup 0 gt } define is_lt : ('A 'b 'c -> 'A 'b bool) {{ tags: level1,boolean }} { dupd lt } define is_lteq : ('A 'b 'c -> 'A 'b bool) {{ tags: level1,boolean }} { dupd lteq } define is_lteqf : ('A 'b -> 'A ('C 'd -> 'C 'd bool)) {{ }} { [is_lteq] papply } define is_lteqz : ('A 'b -> 'A 'b bool) {{ tags: level1,boolean }} { dup 0 lteq } define is_ltf : ('A 'b -> 'A ('C 'd -> 'C 'd bool)) {{ }} { [is_lt] papply } define is_ltz : ('A 'b -> 'A 'b bool) {{ tags: level1,boolean }} { dup 0 lt } define is_neq : ('A 'b 'b -> 'A 'b bool) {{ tags: level1,boolean }} { dupd neq } define is_neqf : ('A 'b -> 'A ('C 'b -> 'C 'b bool)) {{ }} { [is_neq] papply } define is_neqz : ('A int -> 'A int bool) {{ tags: level1,boolean }} { dup 0 neq } define join : ('A list list -> 'A list) {{ desc: Performs an inner join on two lists of pairs. This is the same as concatenat ing two list and performing a self_join. test: in: [1 "a" pair 2 "b" pair] list [3 "a" pair 4 "b" pair] list join out: [[4 2] list "b" pair [3 1] list "a" pair] list tags: level2,hash }} { cat self_join } define keep : ('A ('A -> 'B) -> 'B ('A -> 'B)) {{ desc: Applies a function to the stack, but does not discard the function semantics: [$A] keep == $A [$A] test: in: 1 2 [add_int] keep out: 3 [add_int] tags: level1,functions }} { dup [apply] dip } define line_to : ('A 'b 'c -> 'A) {{ }} { unit swons 0 cons 0 cons Line render } define list : (( -> 'A) -> list) {{ desc: creates a list from a function tags: level0,lists }} { _primitive_ } define list_to_hash : ('A list -> 'A hash_list) {{ desc: Converts a list of pairs into a hash list test: in: [1 "a" pair 2 "b" pair 3 "c" pair] list list_to_hash "a" hash_get popd out: [1] list tags: level2,hash }} { hash_list swap [unpair hash_add_chain] foreach } define list_to_str : ('A CatList -> 'A string) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define ln : ('A double -> 'A double) {{ }} { ln_dbl } define ln_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define log : ('A double double -> 'A double) {{ }} { log_dbl } define log_dbl : ('A double double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define log10 : ('A double -> 'A double) {{ }} { log10_dbl } define log10_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define lt : ('A 'b 'c -> 'A bool) {{ desc: Pushes true if the top value is less than the second value, false otherwise. tags: level1,math }} { [[lt_int] int_type [lt_dbl] double_type [lt_byte] byte_type [lt_bit] bit_type [l t_str] string_type] list dispatch2 as_bool } define lt_bit : ('A Bit Bit -> 'A bool) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define lt_byte : ('A Byte Byte -> 'A bool) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define lt_dbl : ('A double double -> 'A bool) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define lt_int : (int int -> bool) {{ desc: replaces the top two values (integers) with true, if the second value is les s than the top value, otherwise pushes false tags: level0,math }} { _primitive_ } define lt_str : ('A string string -> 'A bool) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define lteq : ('A 'b 'c -> 'A bool) {{ desc: Pushes true if the top value is less than or equal to the second value, fals e otherwise. tags: level1,math }} { [[lteq_int] int_type [lteq_dbl] double_type [lteq_byte] byte_type [lteq_bit] bit _type [lteq_str] string_type] list dispatch2 as_bool } define lteq_bit : ('A Bit Bit -> 'A bool) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define lteq_byte : ('A Byte Byte -> 'A bool) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define lteq_dbl : ('A double double -> 'A bool) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define lteq_int : (int int -> bool) {{ desc: replaces the top two values (integers) with true, if the second value is les s than or equal to the top value, otherwise pushes false tags: level0,math }} { _primitive_ } define lteq_str : ('A string string -> 'A bool) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define lteqf : ('A 'b -> 'A ('C 'd -> 'C bool)) {{ }} { [lteq] papply } define lteqz : ('A int -> 'A bool) {{ tags: level1,boolean }} { 0 lteq_int } define ltf : ('A 'b -> 'A ('C 'd -> 'C bool)) {{ }} { [lt] papply } define ltz : ('A int -> 'A bool) {{ tags: level1,boolean }} { 0 lt_int } define m : ('A ('A self -> 'B) -> 'B) {{ desc: The self application combinator, sometimes referred to as the U combinator tags: demo,fixpoint }} { dup apply } define m_fact : ('A int -> 'A int) {{ desc: A factorial written using the m (a.k.a. U) combinator test: in: 5 m_fact out: 120 tags: demo,fixpoint }} { [over 0 eq [pop2 1] [[dup dec] dip m mul_int] if] m } define m_while : ('A ('B -> 'C) ('A -> 'B bool) -> 'B) {{ desc: A while function written using the M combinator test: in: 1 5 [[2 mul_int] dip dec] [is_neqz] m_while pop out: 32 tags: demo,fixpoint }} { [dip swap] papply swap [dip m] papply quote compose [[pop] if] compose m } define map : (list ('a -> 'b) -> list) {{ desc: creates a list from another by transforming every value using the supplied f unction tags: level0,lists }} { _primitive_ } define map_reduce : ('A list ('B 'c -> 'B 'd) ('E 'f -> 'E 'g) -> 'A list) {{ desc: Takes a list of inputs, a worker function, and on top a way to combine resul ts of two worker functions in an order independent way. This implementation of C at does not do any parallelization, but by expressing an algorithm in this manne r, a clever Cat compiler can automatically distribute and parallelize the work p erformed by the various worker functions. tags: demo,algorithms }} { [map flatten self_join] dip map } define max_bit : ('A Bit Bit -> 'A Bit) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define max_dbl : ('A double double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define max_str : ('A string string -> 'A string) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define min_bit : ('A Bit Bit -> 'A Bit) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define min_dbl : ('A double double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define min_str : ('A string string -> 'A string) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define mod : ('A 'b 'c -> 'A any) {{ desc: Computes the modulo of the top value from the second value. tags: level1,math }} { [[mod_int] int_type [mod_dbl] double_type [mod_byte] byte_type [mod_bit] bit_ typ e] list dispatch2 } define mod_bit : ('A Bit Bit -> 'A Bit) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define mod_byte : ('A Byte Byte -> 'A Byte) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define mod_dbl : ('A double double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define mod_int : (int int -> int) {{ desc: computes the remainder of dividing the top value (an integer) from the secon d value (an integer) tags: level0,math }} { _primitive_ } define modn : ('A int int -> 'A bool) {{ desc: Returns true if the second integer modulo the top integer is equal to zero. test: in: 4 2 modn out: true test: in: 5 3 modn out: false tags: level1,math }} { mod_int eqz } define modnf : ('A int -> 'A ('B int -> 'B bool)) {{ }} { [modn] papply } define move_head : ('A list list -> 'A list list) {{ desc: Copies the head from one list to another. test: in: [1 2] list [3 4] list move_head out: [1 2 4] list [3] list tags: level1,lists }} { uncons swap [cons] dip } define move_to : ('A 'b 'c -> 'A) {{ }} { unit swons Slide render } define mul : ('A 'b 'c -> 'A any) {{ desc: Multiplies top value to second value. tags: level1,math }} { [[mul_int] int_type [mul_dbl] double_type [mul_byte] byte_type [mul_bit] bit_ typ e] list dispatch2 } define mul_bit : ('A Bit Bit -> 'A Bit) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define mul_byte : ('A Byte Byte -> 'A Byte) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define mul_dbl : ('A double double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define mul_int : (int int -> int) {{ desc: multiplies two integers tags: level0,math }} { _primitive_ } define n : ('A int -> 'A list) {{ desc: Constructs a list of consectutive numbers from 0 to n-1 test: in: 4 n out: [3 2 1 0] list tags: level1,lists }} { nil [cons] dig for rev } define neg : ('A 'b -> 'A any) {{ desc: Negates top values. tags: level1,math }} { [[neg_int] int_type [neg_dbl] double_type] list dispatch1 } define neg_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define neg_int : (int -> int) {{ desc: negates the top value tags: level0,math }} { _primitive_ } define negate : ('A ('B -> 'C bool) -> 'A ('B -> 'C bool)) {{ desc: Takes a function that returns a boolean value (i.e. a predicate) and creates a new function that is the negation of the predicate. test: in: 0 [eqz] negate apply out: false tags: level1,math }} { [not] compose } define neq : ('A 'b 'b -> 'A bool) {{ desc: Pushes true if the top two values on the stack are not equal. test: in: 3 5 neq out: true test: in: 'a' 'a' neq out: false tags: level0,boolean }} { eq not } define neq_bit : ('A Bit Bit -> 'A bool) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define neqf : ('A 'b -> 'A ('C 'b -> 'C bool)) {{ }} { [neq] papply } define neqz : ('A int -> 'A bool) {{ desc: Returns true if the top value is zero test: in: 5 neqz out: true test: in: 0 neqz out: false tags: level0,math }} { eqz not } define new_str : ('A char int -> 'A string) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define nil : ( -> list) {{ desc: creates an empty list tags: level0,lists }} { _primitive_ } define noop : ( -> ) {{ desc: Does nothing tags: level0,misc }} { } define not : (bool -> bool) {{ desc: returns true if the top value on the stack is false tags: level0,boolean }} { _primitive_ } define now : ( -> date_time) {{ desc: pushes a value representing the current date and time tags: level2,datetime }} { _primitive_ } define null : ( -> _0) {{ desc: returns the default object with no fields }} { _primitive_ } define odd : ('A int -> 'A bool) {{ tags: level1,boolean }} { even not } define open_window : ( -> ) {{ desc: opens a drawing window tags: level2,graphics }} { _primitive_ } define or : (bool bool -> bool) {{ desc: returns true if either of the top two values on the stack are true tags: level0,boolean }} { _primitive_ } define over : ('A 'b 'c -> 'A 'b 'c 'b) {{ desc: Places a copy of the second item on the top of the stack. semantics: $a $b over == $b $a tags: level1,stack }} { dupd swap } define ow : ( -> ) {{ }} { open_window } define pair : ('A 'b 'c -> 'A list) {{ desc: Creates a list of two items test: in: 1 2 pair out: [1 2] list tags: level1,lists }} { nil bury [cons] dip cons } define papply : ('A 'b ('C 'b -> 'D) -> 'A ('C -> 'D)) {{ desc: partial application: binds the top argument to the top value in the stack tags: level0,functions }} { _primitive_ } define papply2 : ('A 'b 'c ('D 'b 'c -> 'E) -> 'A ('D -> 'E)) {{ desc: Partial application function, binds the top two arguments of a function to t he second and third values on the stack to create a new function. test: in: 3 5 7 [add_int add_int] papply2 apply out: 15 tags: level1,functions }} { papply papply } define papply3 : ('A 'b 'c 'd ('E 'b 'c 'd -> 'F) -> 'A ('E -> 'F)) {{ desc: Binds three arguments of a function to values on the stack. tags: level1,functions }} { papply papply papply } define pattern0 : ('A -> 'A) {{ }} { [100 sq 15 tr] 24 repeat } define pattern1 : ('A -> 'A) {{ }} { [100 sq 15 tr 10 fd] 24 repeat } define pd : ('A -> 'A) {{ }} { false set_pen_up } define pi : ('A -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define point_from_angle : ('A 'b double -> 'A list) {{ desc: Returns a point given an angle and the length of the hypotenuse semantics: point_from_angle(hyp theta) => (get_opposite(hyp theta), get_adjacent(hyp_the ta)) }} { dup2 get_adjacent swap get_opposite pair } define point_on_circle : ('A 'b double double -> 'A list) {{ desc: Returns the location i/nth point of a circle centered around the origin with radius "rad". semantics: point_from_circle(radius, i, n) => point_from_angle(radius, 2 * pi * i / n) }} { div_dbl 2 pi * * point_from_angle } define pop : ('a -> ) {{ desc: removes the top item from the stack tags: level0,stack }} { _primitive_ } define pop2 : ('A 'b 'c -> 'A) {{ desc: Removes two items from the top of the stack. test: in: 1 2 pop2 out: semantics: $a $b pop2 == noop tags: level1,stack }} { pop pop } define pop3 : ('A 'b 'c 'd -> 'A) {{ desc: Removes three item from the top of the stack. test: in: 1 2 3 pop3 out: semantics: $a $b $c pop2 == noop tags: level1,stack }} { pop pop pop } define pop4 : ('A 'b 'c 'd 'e -> 'A) {{ desc: Removes four items from the top of the stack. test: in: 1 2 3 4 pop4 out: semantics: $a $b $c $d pop4 == noop tags: level1,stack }} { pop pop pop pop } define popd : ('A 'b 'c -> 'A 'c) {{ desc: Removes the second item from the stack. test: in: 1 2 popd out: 2 semantics: $a $b popd == $b tags: level0,stack }} { [pop] dip } define popdd : ('A 'b 'c 'd -> 'A 'c 'd) {{ desc: Removes the third item from the stack. test: in: 1 2 3 popdd out: 2 3 semantics: $a $b $c popd == $b $c tags: level1,stack }} { [popd] dip } define pow : ('A double double -> 'A double) {{ }} { pow_dbl } define pow_dbl : ('A double double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define print_list : ('A list -> 'A list) {{ desc: Outputs a list to the console window bugs: The type checker requires variable 'A to be written explicitly tags: level2,io }} { dup [writeln] foreach } define pu : ('A -> 'A) {{ }} { true set_pen_up } define pull : (( -> 'A 'b) -> ( -> 'A) 'b) {{ desc: deconstructs a function tags: experimental }} { _primitive_ } define push : ('A ('B -> 'C) 'd -> 'A ('B -> 'C 'd)) {{ desc: Quotes the top value and composes it with a function below it. test: in: [1] 2 push list out: [1 2] list tags: level2,functions }} { quote compose } define pw : ('A 'b -> 'A) {{ }} { set_pen_width } define qsort : ('A list -> 'A list) {{ desc: This is a naive but simple implementation of a quick sort algorithm. test: in: [5 4 2 1 3 2 4] list qsort out: [5 4 4 3 2 2 1] list test: in: [3 2 1] list qsort out: [3 2 1] list test: in: [1 2 3] list qsort out: [3 2 1] list test: in: [] list qsort out: [] list tags: demo,algorithms }} { [small] [] [uncons under [lt] papply split] [[swap cons] dip cat] bin_rec } define quote : ('a -> ( -> 'a)) {{ desc: creates a constant generating function from the top value on the stack tags: level0,functions }} { _primitive_ } define rcompose : ('A ('B -> 'C) ('D -> 'B) -> 'A ('D -> 'C)) {{ desc: swap then compose (or reverse compose) semantics: [$A] [$B] rcompose == [$B $A] test: in: [1 2] [3 4] rcompose apply out: 3 4 1 2 tags: level1,functions }} { swap compose } define re_find : (string regex -> string int) {{ desc: finds the regular expression tags: clr }} { _primitive_ } define re_input : ( -> string) {{ }} { One car red car blue car } define re_match : (string regex -> list) {{ desc: matches the regular expression tags: clr }} { _primitive_ } define re_pattern : ( -> string) {{ }} { (\w+)\s+(car) } define re_test : ('A -> 'A list) {{ }} { re_input re_pattern regex re_match } define readch : ( -> char) {{ desc: inputs a single character from the console tags: level1,console }} { _primitive_ } define readln : ( -> string) {{ desc: inputs a string from the console tags: level1,console }} { _primitive_ } define red : ('A -> 'A) {{ }} { 255 0 0 rgb } define ref : ('a -> ref) {{ desc: creates a reference tags: level2,ref }} { _primitive_ } define regex : (string -> regex) {{ desc: constructs a regular expression matcher tags: clr }} { _primitive_ } define render : (list string -> ) {{ desc: sends a drawing instruction to the graphics device tags: level2,graphics }} { _primitive_ } define repeat : ('A ('A -> 'A) int -> 'A) {{ desc: Executes a loop a fixed number of times semantics: $A [$B] $c repeat == $A $c eqz [] [$B $c dec] if test: in: 3 [inc] 3 repeat out: 6 test: in: 3 [2 mul_int] 2 repeat out: 12 test: in: 3 [inc] 0 repeat out: 3 tags: level1,control }} { swap [dip dec] papply [dup neqz] while pop } define replace_str : ('A string string string -> 'A string) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define rest : ('A list -> 'A list list) {{ desc: Returns a list with one less item test: in: [1 2 3] list rest out: [1 2 3] list [1 2] list tags: level1,lists }} { dup tail } define rev : ('A list -> 'A list) {{ desc: Reverses a list test: in: [1 2 3] list rev out: [3 2 1] list tags: level1,lists }} { nil [cons] fold } define rfold : ('A list 'b ('A 'b any -> 'C 'd) -> 'A 'b) {{ desc: A reverse fold, it combines adjacent values in a list using an initial value and a binary function, starting with the last value in the list. semantics: define rfold(xs x f) { xs empty dig [x] [uncons f rfold x f apply] if } test: in: [1 2 3 4] list 0 [add_int] rfold out: 10 test: in: [1 2 3 4] list 0 [popd] rfold out: 4 test: in: [1 2 3 4] list nil [cons] rfold out: [1 2 3 4] list tags: level1,lists }} { [rev] dip2 fold } define rgb : ('A 'b 'c 'd -> 'A) {{ }} { triple Rgb render } define rnd_dbl : ( -> double) {{ desc: creates a random floating point number between zero and 1.0 tags: level1,misc }} { _primitive_ } define rnd_int : (int -> int) {{ desc: creates a random integer between zero and some maximum value tags: level1,misc }} { _primitive_ } define round_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define rpapply : ('A ('B 'c -> 'D) 'c -> 'A ('B -> 'D)) {{ desc: Binds the top argument of the function in the second position on the stack t o the value on the top of the stack. test: in: 5 [add_int] 3 rpapply apply out: 8 tags: level1,functions }} { quote swap compose } define save_window : (string -> ) {{ desc: saves a bitmap of the viewport tags: level2,graphics }} { _primitive_ } define self_join : ('A list -> 'A list) {{ desc: Performs an inner self-join on a list of pairs based the first item. In othe r words all unique first items in pairs are treated as keys, and all second valu es are concatenated together in a list. test: in: [1 "a" pair 2 "b" pair 3 "a" pair 4 "b" pair] list self_join out: [[4 2] list "b" pair [3 1] list "a" pair] list tags: level2,hash }} { list_to_hash hash_to_list } define set_at : (list 'a int -> list) {{ desc: sets an item in a list tags: level1,lists }} { _primitive_ } define set_pen_up : ('A 'b -> 'A) {{ }} { unit SetPenUp render } define set_pen_width : ('A 'b -> 'A) {{ }} { unit SetPenWidth render } define set_turtle_visibility : ('A 'b -> 'A) {{ }} { unit SetTurtleVisibility render } define set1 : ('A 'b 'c -> 'A 'c) {{ desc: Replaces the value in the second location on the stack, with the top value o f the stack. test: in: 1 2 set1 out: 2 tags: level1,stack }} { popd } define set2 : ('A 'b 'c 'd -> 'A 'd 'c) {{ desc: Replaces the value in the third location on the stack, with the top value of the stack. test: in: 1 2 3 set2 out: 3 2 tags: level1,stack }} { swap [set1] dip } define set3 : ('A 'b 'c 'd 'e -> 'A 'e 'c 'd) {{ desc: Replaces the value in the fourth location on the stack, with the top value o f the stack. test: in: 1 2 3 4 set3 out: 4 2 3 tags: level1,stack }} { swap [set2] dip } define set4 : ('A 'b 'c 'd 'e 'f -> 'A 'f 'c 'd 'e) {{ desc: Replaces the value in the fifth location on the stack, with the top value of the stack. test: in: 1 2 3 4 5 set4 out: 5 2 3 4 tags: level1,stack }} { swap [set3] dip } define setval : (ref any -> ref) {{ desc: sets a reference to a new value tags: level2,ref }} { _primitive_ } define shl_byte : ('A Byte Byte -> 'A Byte) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define shl_int : (int int -> int) {{ desc: shifts the second value left by the number of bits indicated on the top of t he stack tags: level0,math }} { _primitive_ } define show_turtle : ('A -> 'A) {{ }} { true set_turtle_visibility } define shr_byte : ('A Byte Byte -> 'A Byte) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define shr_int : (int int -> int) {{ desc: shifts the second value left by the number of bits indicated on the top of t he stack tags: level0,math }} { _primitive_ } define sin : ('A double -> 'A double) {{ }} { sin_dbl } define sin_cos : ('A double -> 'A double double) {{ }} { dup sin swap cos } define sin_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define sinh : ('A double -> 'A double) {{ }} { sinh_dbl } define sinh_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define slip : ('A ('A -> 'B) 'c -> 'B 'c) {{ desc: Applies a function below the top item on the stack to the stack below it. semantics: $A [$B] $c slip == $A $B $c test: in: 4 [1 add_int] 2 slip out: 5 2 tags: level1,functions }} { swap dip } define small : ('A list -> 'A list bool) {{ desc: Returns true if a list contains one element or is empty test: in: 1 unit small popd out: true tags: level1,lists }} { empty [true] [rest empty popd] if } define split : ('A list ('A list list any any -> 'B list list 'c bool) -> 'A list list) {{ desc: Splits a list into two new lists test: in: [0 1 2 3 4] list [even] split out: [1 3] list [0 2 4] list tags: level1,lists }} { nil bury nil swap [dupd apply [cons] [swapd cons swap] if] papply rfold } define split_at : ('A list int -> 'A list list) {{ desc: Splits a list in two at an index, the top result contains the second part an d the second result contains the first part of the original list test: in: [1 2 3 4] list 1 split_at out: [4] list [1 2 3] list tags: level1,lists }} { nil bury [move_head] swap repeat } define sq : ('A 'b -> 'A) {{ }} { draw_square } define sqr : ('A 'b -> 'A any) {{ desc: Multiplies a value by itself test: in: 3 sqr out: 9 tags: level1,math }} { dup mul } define sqr_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define sqrt : ('A double -> 'A double) {{ }} { sqrt_dbl } define sqrt_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define st : ('A -> 'A) {{ }} { show_turtle } define str : (any -> string) {{ desc: converts any value into a string representation. tags: level2,strings }} { _primitive_ } define str_to_list : ('A string -> 'A CatList) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define string_type : ( -> type) {{ desc: pushes a value representing the type of a string tags: level1,types }} { _primitive_ } define sub : ('A 'b 'c -> 'A any) {{ desc: Subtracts top value from second value. tags: level1,math }} { [[sub_int] int_type [sub_dbl] double_type [sub_byte] byte_type [sub_bit] bit_ typ e] list dispatch2 } define sub_bit : ('A Bit Bit -> 'A Bit) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define sub_byte : ('A Byte Byte -> 'A Byte) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define sub_dbl : ('A double double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define sub_int : (int int -> int) {{ desc: subtracts the top value (an integer) from the second value (an integer) tags: level0,math }} { _primitive_ } define sub_str : ('A string int int -> 'A string) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define sub_time : (date_time date_time -> time_span) {{ desc: computes the time period between two dates tags: level2,datetime }} { _primitive_ } define sw : (string -> ) {{ }} { save_window } define swap : ('a 'b -> 'b 'a) {{ desc: swap the top two items on the stack tags: level0,stack }} { _primitive_ } define swap_at : (list any int -> list any) {{ desc: swaps an item, with an item in the list tags: level1,lists }} { _primitive_ } define swapd : ('A 'b 'c 'd -> 'A 'c 'b 'd) {{ desc: Swaps the second and third items on the stack. test: in: 1 2 3 swapd out: 2 1 3 semantics: $a $b $c swapd == $b $a $c tags: level0,stack }} { [swap] dip } define swapdd : ('A 'b 'c 'd 'e -> 'A 'c 'b 'd 'e) {{ desc: Swaps the third and fourth items on the stack. test: in: 1 2 3 4 swapdd out: 2 1 3 4 semantics: $a $b $c $d swapd == $b $a $c $d tags: level1,stack }} { [swapd] dip } define swons : ('A 'b list -> 'A list) {{ desc: Appends the second item on the stack, to a list in the first item test: in: 3 [1 2] list swons out: [1 2 3] list tags: level1,lists }} { swap cons } define tail : ('A list -> 'A list) {{ desc: Removes first item from the list test: in: [1 2 3] list tail out: [1 2] list tags: level1,lists }} { uncons pop } define tan : ('A double -> 'A double) {{ }} { tan_dbl } define tan_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define tanh : ('A double -> 'A double) {{ }} { tanh_dbl } define tanh_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define temp_file : ( -> string) {{ desc: creates a unique temporary file tags: level2,streams }} { _primitive_ } define test_m : ('A list -> 'A list) {{ }} { unpair pop [1 swap pair] map } define test_map_reduce : ('A -> 'A list) {{ }} { test_strings [test_m] [test_r] map_reduce print_list } define test_r : ('A list -> 'A list) {{ }} { unpair [vec_sum] dip pair } define test_strings : ('A -> 'A list) {{ }} { [[The quick brown fox jumped over the lazy dog] list 1 pair [I am very lazy] lis t 2 pair [I hope this is over quick] list 3 pair [I have high hopes for the lazy dog] list 4 pair] list } define throw : (any -> ) {{ desc: throws an exception tags: level2,control }} { _primitive_ } define throw_if : ('A bool any -> 'A) {{ desc: A utilty for throwing exceptions tags: level1,misc }} { [throw] [quote] dip compose [] if } define tl : ('A 'b -> 'A) {{ }} { turn_left } define to_bool : (any -> bool) {{ desc: coerces any value to a boolean tags: level1,conversion }} { _primitive_ } define to_int : (any -> int) {{ desc: coerces any value to an integer tags: level1,conversion }} { _primitive_ } define to_msec : (time_span -> int) {{ desc: computes the length of a time span in milliseconds tags: level2,datetime }} { _primitive_ } define to_str : (any -> str) {{ desc: coerces any value to a string tags: level1,conversion }} { _primitive_ } define tr : ('A 'b -> 'A) {{ }} { turn_right } define triple : ('A 'b 'c 'd -> 'A list) {{ desc: Creates a list of three items test: in: 1 2 3 triple out: [1 2 3] list tags: level1,lists }} { [pair] dip cons } define true : ( -> bool) {{ desc: pushes the boolean value true on the stack tags: level0,boolean }} { _primitive_ } define trunc_dbl : ('A double -> 'A double) {{ desc: undocumented method from CLR tags: level2 }} { _primitive_ } define try : ('A ('B -> 'B 'C) -> 'A 'C) {{ desc: Provides a simple default exception handler tags: level1,misc }} { [exception data: write writeln] try_catch } define try_catch : (( -> 'A) (exception -> 'A) -> 'A) {{ desc: evaluates a function, and catches any exceptions tags: level2,control }} { _primitive_ } define turn_left : ('A 'b -> 'A) {{ }} { neg tr } define turn_right : ('A 'b -> 'A) {{ }} { unit Rotate render } define type_eq : (type type -> bool) {{ desc: returns true if either type is assignable to the other tags: level1,types }} { _primitive_ } define type_type : ( -> type) {{ desc: pushes a value representing the type of a type tags: level1,types }} { _primitive_ } define typename : (any -> string) {{ desc: returns the name of the type of an object tags: level1,types }} { _primitive_ } define typeof : (any -> any type) {{ desc: returns a type tag for an object tags: level1,types }} { _primitive_ } define uncons : (list -> list any) {{ desc: returns the top of the list, and the rest of a list tags: level0,lists }} { _primitive_ } define uncons2 : ('A list -> 'A list any any) {{ desc: Extracts the first and second item from a list test: in: [1 2 3] list uncons2 out: [1] list 2 3 tags: level1,lists }} { uncons [uncons] dip } define under : ('A 'b 'c -> 'A 'c 'b 'c) {{ desc: Places a copy of the top item on the stack under the second. test: in: 1 2 under out: 2 1 2 tags: level1,stack }} { dup swapd } define under3 : ('A 'b 'c 'd -> 'A 'd 'b 'c 'd) {{ desc: Moves a copy of the top of the stack under the third item tests: in: 1 2 3 under3 out: 3 1 2 3 tags: level1,stack }} { swap [under] dip swap } define under4 : ('A 'b 'c 'd 'e -> 'A 'e 'b 'c 'd 'e) {{ desc: Moves a copy of the top of the stack under the fourth item. tests: in: 1 2 3 4 under4 out: 4 1 2 3 4 tags: level1,stack }} { swap [under3] dip swap } define under5 : ('A 'b 'c 'd 'e 'f -> 'A 'f 'b 'c 'd 'e 'f) {{ desc: Moves a copy of the top of the stack under the fifth item. tests: in: 1 2 3 4 5 under5 out: 5 1 2 3 4 5 tags: level1,stack }} { swap [under4] dip swap } define unit : ('A 'b -> 'A list) {{ desc: Creates a list containing one element, which is taken from the top of the st ack test: in: 42 unit out: [42] list tags: level1,lists }} { nil swons } define unpair : ('A list -> 'A any any) {{ desc: Extracts the first and second item from a list test: in: [1 2] list unpair out: 1 2 tags: level1,lists }} { uncons [head] dip } define vec_scale : ('A list 'b -> 'A list) {{ desc: Multiplies all items in a vector by a scalar value. test: in: [0 1 2] list 2 vec_scale out: [0 2 4] list tags: level1,vectors }} { [*] papply map } define vec_slide : ('A list 'b -> 'A list) {{ desc: Adds a value to all items in a vector. test: in: [0 1 2] list 2 vec_slide out: [2 3 4] list tags: level1,vectors }} { [+] papply map } define vec_sum : ('A list -> 'A int) {{ desc: Computes the sum of elements in a vector. test: in: [0 1 2] list vec_sum out: 3 tags: level1,vectors }} { 0 [+] fold } define while : ('A ('A -> 'A) ('A -> 'A bool) -> 'A) {{ desc: executes a block of code repeatedly until the condition returns true tags: level1,control }} { _primitive_ } define whilen : ('A ('A -> 'A) ('A -> 'A bool) -> 'A) {{ desc: Executes a while loop, while a condition is not true semantics: $A [$B] [$C] whilen == $A $C not [$B [$B][$C] whilen] [] if test: in: 42 [dec] [dup eqz] whilen out: 0 tags: level2,control }} { negate while } define whilene : ('A list ('A list -> 'A list) -> 'A) {{ desc: Executes a function while the list on the top of the stack is not empty semantics: $A $b [$C] whilene == $A $b empty not [$C [$C] whilene] [pop] if test: in: 0 [1 2 3 4] list [uncons swap [add_int] dip] whilene out: 10 tags: level2,control }} { [empty] whilen pop } define whilenz : ('A int ('A int -> 'A int) -> 'A) {{ desc: Executes a function while the value on the top of the stack is not equal to zero. semantics: $A $b [$C] whilenz == $A $b neqz [$C [$C] whilenz] [pop] if test: in: 1 4 [[2 mul_int] dip dec] whilenz out: 16 tags: level2,control }} { [dup neqz] while pop } define write : ('a -> ) {{ desc: outputs the text representation of a value to the console tags: level1,console }} { _primitive_ } define writeln : ('a -> ) {{ desc: outputs the text representation of a value to the console followed by a newl ine character tags: level1,console }} { _primitive_ } define y : ('A ('A ('A -> 'B) -> 'B) -> 'B) {{ desc: This is the famous y combinator. It executes a function with itself as an ar gument. The function is expected to terminate on its own when a "fixpoint" is re ached. tags: demo,fixpoint }} { [dup papply] swap compose dup apply } define y_fact : ('A int int int -> 'A int int int) {{ desc: A factorial written using the y combinator test: in: 5 y_fact out: 120 tags: demo,fixpoint }} { [dupd swap 0 eq [pop2 1] [[dup dec] dip apply mul_int] if] y }
|