First class environments: Difference between revisions

Content added Content deleted
(Added Bracmat)
No edit summary
Line 420: Line 420:
0 1 7 2 5 8 16 3 19 6 14 9</lang>
0 1 7 2 5 8 16 3 19 6 14 9</lang>
In essence: run is a static method of the class <code>hailstone</code> which, given a list of objects of the class runs all of them until their hailstone sequence number stops changing. It also displays the hailstone sequence number from each of the objects at each step. Its result is the step count from each object.
In essence: run is a static method of the class <code>hailstone</code> which, given a list of objects of the class runs all of them until their hailstone sequence number stops changing. It also displays the hailstone sequence number from each of the objects at each step. Its result is the step count from each object.

=={{header|Order}}==
Order supports environments as a first-class type, but since all values are immutable, updating a value means using one environment to update the next in a chain (nothing unusual for languages with immutable data structures):
<lang c>#include <order/interpreter.h>
#define ORDER_PP_DEF_8hail ORDER_PP_FN( \
8fn(8N, 8cond((8equal(8N, 1), 1) \
(8is_0(8remainder(8N, 2)), 8quotient(8N, 2)) \
(8else, 8inc(8times(8N, 3))))) )

#define ORDER_PP_DEF_8h_loop ORDER_PP_FN( \
8fn(8S, \
8let((8F, 8fn(8E, 8env_ref(8(8H), 8E))), \
8do( \
8print(8seq_to_tuple(8seq_map(8F, 8S)) 8space), \
8let((8S, 8h_once(8S)), \
8if(8equal(1, \
8seq_fold(8times, 1, 8seq_map(8F, 8S))), \
8print_counts(8S), \
8h_loop(8S)))))) )

#define ORDER_PP_DEF_8h_once ORDER_PP_FN( \
8fn(8S, \
8seq_map( \
8fn(8E, \
8eval(8E, \
8quote( \
8env_bind(8(8C), \
8env_bind(8(8H), \
8env_bind(8(8E), 8E, 8E), \
8hail(8H)), \
8if(8equal(8H, 1), 8C, 8inc(8C))) ))), \
8S)) )

#define ORDER_PP_DEF_8print_counts ORDER_PP_FN( \
8fn(8S, \
8print(8space 8(Counts:) \
8seq_to_tuple(8seq_map(8fn(8E, 8env_ref(8(8C), 8E)), 8S)))) )

ORDER_PP(
8let((8S, // Build a list of environments
8seq_map(8fn(8N, 8seq_of_pairs_to_env(
8seq(8pair(8(8H), 8N), 8pair(8(8C), 0),
8pair(8(8E), 8env_nil)))),
8seq_iota(1, 13))),
8h_loop(8S))
)</lang>
{{out}}
<lang>(1,2,3,4,5,6,7,8,9,10,11,12) (1,1,10,2,16,3,22,4,28,5,34,6) (1,1,5,1,8,10,11,2,14,16,17,3) (1,1,16,1,4,5,34,1,7,8,52,10) (1,1,8,1,2,16,17,1,22,4,26,5) (1,1,4,1,1,8,52,1,11,2,13,16) (1,1,2,1,1,4,26,1,34,1,40,8) (1,1,1,1,1,2,13,1,17,1,20,4) (1,1,1,1,1,1,40,1,52,1,10,2) (1,1,1,1,1,1,20,1,26,1,5,1) (1,1,1,1,1,1,10,1,13,1,16,1) (1,1,1,1,1,1,5,1,40,1,8,1) (1,1,1,1,1,1,16,1,20,1,4,1) (1,1,1,1,1,1,8,1,10,1,2,1) (1,1,1,1,1,1,4,1,5,1,1,1) (1,1,1,1,1,1,2,1,16,1,1,1) (1,1,1,1,1,1,1,1,8,1,1,1) (1,1,1,1,1,1,1,1,4,1,1,1) (1,1,1,1,1,1,1,1,2,1,1,1) Counts:(0,1,7,2,5,8,16,3,19,6,14,9)</lang>
The C preprocessor cannot output newlines, so the output is all on one line, but easily parsable.


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==