-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
520 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
42minjoy.tar | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,276 @@ | ||
1 %LISTING 1 | ||
2 (* | ||
3 J O Y T U T O R I A L | ||
4 | ||
5 JOY is a functional language which uses postfix-like notation | ||
6 and operates on a stack. Its base types are Booleans, | ||
7 characters, integers and symbols. A list of values | ||
8 of any type is a value of type list. | ||
9 *) | ||
10 | ||
11 (* push two numbers onto stack, add them, write result *) | ||
12 [ 111 222 + put ] | ||
333 | ||
13 (* add two numbers, add another two numbers, write product *) | ||
14 [ 1 2 + 3 4 + * put ] | ||
21 | ||
15 (* testing whether 2 + 2 = 2 * 2 *) | ||
16 [ 2 2 + 2 2 * = put ] | ||
true | ||
17 (* testing whether 6 * 6 > 5 * 7 *) | ||
18 [ 6 6 * 5 7 * > put ] | ||
true | ||
19 (* Boolean operations *) | ||
20 [ true false or true and not put ] | ||
false | ||
21 | ||
22 (* LISTS *) | ||
23 | ||
24 (* push a list of numbers, reverse it, write result *) | ||
25 [ [1 2 3 4 5] reverse put ] | ||
[5 4 3 2 1] | ||
26 (* push two lists of symbols, concatenate, write result *) | ||
27 [ [peter paul] [mary jane] concat put ] | ||
[peter paul mary jane] | ||
28 (* push a list of mixed values, write its last element *) | ||
29 [ [11 false 'X 44] last put ] | ||
44 | ||
30 (* push a number and a list, determine membership *) | ||
31 [ 3 [1 5 3 4 2] member put ] | ||
true | ||
32 (* similar *) | ||
33 [ 3 [1 5 6 4 2] member put ] | ||
false | ||
34 (* push a list of numbers, duplicate to find sum and product *) | ||
35 [ [1 2 3 4] dup sum put space put product put ] | ||
10 24 | ||
36 (* push a number and a list of numbers, cons together *) | ||
37 [ 111 [ 222 333 ] cons put ] | ||
[111 222 333] | ||
38 (* push a list, uncons twice, write remainder and first two *) | ||
39 [ [11 22 33 44 55] uncons uncons putsp putsp putsp ] | ||
[33 44 55] 22 11 | ||
40 (* push two lists of characters, concatenate them *) | ||
41 [ [ 'a 'b ] [ 'd 'e 'f ] concat ] | ||
42 (* now write result, but dup first so list is not lost *) | ||
43 [ dup put ] | ||
[a b d e f] | ||
44 (* insert the missing 'c *) | ||
45 [ uncons uncons 'c swap cons cons cons ] | ||
46 (* now check *) | ||
47 [ dup put ] | ||
[a b c d e f] | ||
48 (* what is its length ? *) | ||
49 [ dup length put ] | ||
6 | ||
50 (* reverse it, write its length *) | ||
51 [ reverse length put ] | ||
6 | ||
52 (* So, the length of a list is also the length of its reverse: | ||
53 length == reverse length | ||
54 *) | ||
55 (* INPUT from terminal or input file *) | ||
56 [ get get + put ] | ||
57 123 456 | ||
579 | ||
58 (* COMBINATORS *) | ||
59 | ||
60 (* | ||
61 Combinators are operations which expect a list on top | ||
62 of the stack and then execute it as a program. | ||
63 *) | ||
64 (* push two numbers and a program, i-combinator to execute *) | ||
65 [ 111 222 [+ put] i ] | ||
333 | ||
66 (* i-combinator to execute [+ put] on top of stack *) | ||
67 [ 111 [put +] reverse 222 swap i ] | ||
333 | ||
68 (* dip-combinator to multiply 3 and 7, then add 100 *) | ||
69 [ 3 7 100 [*] dip + put ] | ||
121 | ||
70 (* step-combinator to apply program to each member of list *) | ||
71 [ [1 2 3] [dup * putsp] step ] | ||
1 4 9 | ||
72 | ||
73 (* i-combinator, twice-combinator, thrice-combinator *) | ||
74 [ 2 [dup *] i put ] | ||
4 | ||
75 [ 2 [dup *] twice put ] | ||
16 | ||
76 [ 2 [dup *] thrice put ] | ||
256 | ||
77 (* times-combinator, using definition square == dup * *) | ||
78 [ 2 [square] 0 times put ] | ||
2 | ||
79 [ 2 [square] 1 times put ] | ||
4 | ||
80 [ 2 [square] 2 times put ] | ||
16 | ||
81 [ 2 [square] 3 times put ] | ||
256 | ||
82 [ 2 [square] 4 times put ] | ||
65536 | ||
83 [ 2 [square] 5 times put ] (* note overflow *) | ||
4294967296 | ||
84 [ [7] 10 times stack put ] | ||
[7 7 7 7 7 7 7 7 7 7] | ||
85 | ||
86 (* map-combinator to make list of squares *) | ||
87 [ [1 2 3] [dup *] map put ] | ||
[1 4 9] | ||
88 (* fold-combinator to add squares of members of list *) | ||
89 [ [1 2 3] 0 [dup * +] fold put ] | ||
14 | ||
90 (* construct-combinator to make list from programs *) | ||
91 [ 11 12 (* push two numbers *) | ||
92 [ (* make a list of .. *) | ||
93 [+] (* their sum *) | ||
94 [*] (* their product *) | ||
95 [pop unit] (* the unit list of first *) | ||
96 [dup pair] ] (* the pair of the second *) | ||
97 construct2 put ] | ||
[23 132 [11] [12 12]] | ||
98 (* the two numbers are still there *) | ||
99 [ [ (* make a list of .. *) | ||
100 [pair [square] map unpair +](* the sum of their squares *) | ||
101 [pop] (* the first number *) | ||
102 [] ] (* the second number *) | ||
103 construct2 put ] | ||
[265 11 12] | ||
104 (* now clear the stack *) | ||
105 [ [] unstack ] | ||
106 | ||
107 (* DIRECTIVES *) | ||
108 | ||
109 %INCLUDE 42minjoy.in1 | ||
1 (* vi: filetype=joy | ||
2 begin of include file *) | ||
3 | ||
4 (* SET- and IF-DIRECTIVES *) | ||
5 | ||
6 %SET X = 1 | ||
7 %IF = X 1 [ 11111 ] | ||
8 %IF = X 2 [ 22222 ] | ||
9 [ put ] | ||
11111 | ||
10 | ||
11 (* ALTERNATIVE RADIX for input numbers *) | ||
12 | ||
13 (* default alternative radix is 2 *) | ||
14 [ &1000000 put ] | ||
64 | ||
15 (* change default alternative radix *) %RADIX 8 | ||
16 [ &100 put ] | ||
64 | ||
17 (* change default alternative radix *) %RADIX 16 | ||
18 [ &FF put ] | ||
255 | ||
19 | ||
20 (* SCAN-TIME EXPRESSIONS IN CHARACTER CONSTANTS *) | ||
21 | ||
22 %SET L = 65 | ||
23 [ '\L put ] | ||
A | ||
24 [ '\ + L 32 put ] | ||
a | ||
25 %SET L = 'G | ||
26 [ '\ + L - 'a 'A put ] | ||
g | ||
27 | ||
28 (* end of include file *) | ||
110 (* back to original line numbering *) | ||
111 | ||
112 %INCLUDE 42minjoy.in2 | ||
1 (* vi: filetype=joy | ||
2 *) | ||
3 (* RECURSIVE FUNCTIONS, non-recursive computation *) | ||
4 | ||
5 (* "last" is a tail-recursive function *) | ||
6 [ [ Smith Jones Robinson ] last put ] | ||
Robinson | ||
7 (* now let us look at the (recursive) definition of "last" *) | ||
8 [ [last] definition put ] | ||
[dup rest null [first] [rest last] branch] | ||
9 [ [ Smith Jones Robinson ] [last] definition i put ] | ||
Robinson | ||
10 (* using the x-combinator *) | ||
11 [ [Smith Jones Robinson] | ||
12 [ swap dup rest null | ||
13 [ car swap pop ] | ||
14 [ cdr swap x ] (* NOTE x-combinator *) | ||
15 branch ] | ||
16 x put ] (* REPEAT x-combinator *) | ||
Robinson | ||
17 (* using the y-combinator *) | ||
18 [ [Smith Jones Robinson] | ||
19 [ swap dup rest null | ||
20 [ car swap pop ] | ||
21 [ cdr swap i ] (* NOTE i-combinator *) | ||
22 branch ] | ||
23 y put ] (* NOTE y-combinator *) | ||
Robinson | ||
24 | ||
25 (* "factorial" is not tail-recursive *) | ||
26 [ 6 factorial_rec put ] | ||
720 | ||
27 (* using the x-combinator *) | ||
28 [ 6 | ||
29 [ swap dup 1 <= | ||
30 [ pop pop 1 ] | ||
31 [ dup pred rolldown x * ] | ||
32 branch ] | ||
33 x put ] | ||
720 | ||
34 (* using the y-combinator *) | ||
35 [ 6 | ||
36 [ swap dup 1 <= | ||
37 [ pop pop 1 ] | ||
38 [ dup pred rolldown i * ] | ||
39 branch ] | ||
40 y put ] | ||
720 | ||
41 | ||
42 (* "QUICKSORT" *) | ||
43 | ||
44 [ [1 9 2 8 3 7 4 6 5] quicksort putln ] | ||
[1 2 3 4 5 6 7 8 9] | ||
45 [ [5 6 4 7 3 8 2 9 1] quicksort putln ] | ||
[1 2 3 4 5 6 7 8 9] | ||
46 [ [1 2 3 4 5 6 7 8 9] quicksort putln ] | ||
[1 2 3 4 5 6 7 8 9] | ||
47 [ [9 8 7 6 5 4 3 2 1] quicksort putln ] | ||
[1 2 3 4 5 6 7 8 9] | ||
48 | ||
49 (* now look at the definition of quicksort: *) | ||
50 [ [quicksort] definition putln ] | ||
[dup small [] [partition quicksort [quicksort] dip concat] | ||
branch] | ||
51 | ||
52 (* sorting lists on first item *) | ||
53 [ [ [1 Smith] [3 Jones] [2 Robinson] [4 Brown] ] | ||
54 quicksort1 putln ] | ||
[[1 Smith] [2 Robinson] [3 Jones] [4 Brown]] | ||
55 (* sorting on symbol *) | ||
56 [ [] ] (* initial class-list in COMPUTATIONAL CHRONOSCOPY *) | ||
57 [ [NURKS Peter 1989 year 3 major Computer Science ] cnos | ||
58 [ABELSON Mary 1990 year 2 major Logic ] cnos | ||
59 [ZEEMAN Fred 1988 year 2 major Accounting] cnos | ||
60 [MORRIS Janna 1992 year 1 major undecided] cnos ] | ||
61 (* now sort on surname and print *) | ||
62 [ quicksort1 dup [putln] step ] | ||
[ABELSON Mary 1990 year 2 major Logic] | ||
[MORRIS Janna 1992 year 1 major undecided] | ||
[NURKS Peter 1989 year 3 major Computer Science] | ||
[ZEEMAN Fred 1988 year 2 major Accounting] | ||
113 | ||
114 %STATISTICS 1 | ||
115 (* end of JOY tutorial *) | ||
116 . | ||
0 milliseconds CPU | ||
0 milliseconds CPU to read library | ||
0 milliseconds CPU to execute | ||
1439 user nodes available | ||
13 garbage collections | ||
18881 nodes used | ||
5993 calls to joy interpreter | ||
16240 operations executed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ will be: | |
|
||
gdb joy | ||
... | ||
run 42minjoy.lib tutorial.joy | ||
run tutorial.joy | ||
... | ||
quit | ||
|
||
|
Oops, something went wrong.