source: project/wiki/language-comparison @ 36676

Last change on this file since 36676 was 31126, checked in by Mario Domenech Goulart, 5 years ago

Properly capitalize CHICKEN on the wiki directory (only first level).

I used the following shell script to change things:

while IFS= read -d $'\0' -r file ; do

sed -i 's/Chicken/CHICKEN/g' "$file"

done < <(find wiki -maxdepth 1 -type f -print0 )

Some files have been manually reverted after that, since some
substitutions don't apply:

  • friedly-chicken (repl banner)
  • survey2011 (Chicken in URI paths)
  • chickenista-guide (Chickenista)

I hope the link canonicalization thing will be on my side.

File size: 7.0 KB
Line 
1== Input & output
2
3<table>
4
5<tr>
6<td></td>
7<td>CHICKEN</td>
8<td>C</td>
9<td>Python</td>
10<td>Perl</td>
11</tr>
12
13<tr>
14<td>Formatting and printing to stdout</td>
15<td><enscript highlight="scheme">(format #t "I want ~A ~A~%" count desc)</enscript></td>
16<td>printf("I want %d %s\n", count, desc);</td>
17<td><enscript highlight="python">print("I want {} {}\n".format(count, desc))</enscript></td>
18<td><enscript highlight="perl">say "I want $count $desc";</enscript></td>
19</tr>
20
21<tr>
22<td>Printing to stderr</td>
23<td><enscript highlight="scheme">(format (current-error-port) "Ooops~%")</enscript></td>
24<td>fprintf(stderr, "Ooops\n");</td>
25<td>...</td>
26<td><enscript highlight="perl">say STDERR 'Ooops';</enscript></td>
27</tr>
28
29<tr>
30<td>Reading a line from stdin</td>
31<td><enscript highlight="scheme">(read-line)</enscript></td>
32<td><enscript highlight="c">char buffer[1024];
33fgets(buffer, sizeof(buffer), stdin);</enscript></td>
34<td>...</td>
35<td><enscript highlight="perl">&lt;STDIN&gt;</enscript></td>
36</tr>
37
38
39<tr>
40<td>Opening a file for input</td>
41<td><enscript highlight="scheme">(open-input-file path)</enscript></td>
42<td><enscript highlight="c">fopen(path, "r");</enscript></td>
43<td>...</td>
44<td><enscript highlight="perl">open my $fh, '&lt;', $path;</enscript></td>
45</tr>
46
47
48<tr>
49<td>Opening a file for output</td>
50<td><enscript highlight="scheme">(open-output-file path)</enscript></td>
51<td><enscript highlight="c">fopen(path, "w");</enscript></td>
52<td>...</td>
53<td><enscript highlight="perl">open my $fh, '&gt;', $path;</enscript></td>
54</tr>
55
56
57<tr>
58<td>Count lines in a file</td>
59<td><enscript highlight="scheme">(with-input-from-file path
60  (lambda ()
61    (let loop ((count 0))
62      (if (eof-object? (read-line))
63        count
64        (loop (+ count 1)))))))</enscript>
65</td>
66<td><enscript highlight="c">FILE *in = fopen(path, "r");
67int c, count = 0;
68if (!in)
69  handle_error();
70while ((c = fgetc(in)) != EOF)
71  if (c == '\n')
72    lines ++;
73fclose(in);
74return count;</enscript></td>
75<td>...</td>
76<td><enscript highlight="perl">
77open my $fh, '&lt;', $path;
78my $count = &gt;$fh&lt;;
79close $fh;
80</enscript</td>
81</tr>
82
83<tr>
84<td>
85Counting lines in a file if you don't want to keep an entire line in memory at any time:
86<td>
87<enscript highlight="scheme">(with-input-from-file path
88  (lambda ()
89    (let loop ((count 0) (c (read-char)))
90      (if (eof-object? c)
91        count
92        (loop (if (char=? c #\newline) (+ count 1) count) (read-char))))))</enscript>
93</td>
94<td>...</td>
95<td>
96<enscript highlight="python">f=open("file.txt", "rt")
97count=0
98while True:
99    c= f.read(1)
100    if c == "":
101        break
102    if c == "\n":
103        count = count + 1</enscript>
104</td>
105<td>...</td>
106</tr>
107
108<tr>
109<td>Write a file with some numbers
110
111In practice you'd probably collapse the calls to format and equivalent functions in a single call.
112</td>
113<td><enscript highlight="scheme">(with-output-to-file path
114  (lambda ()
115    (format #t "~A~%" 1)
116    (format #t "~A~%" 2)
117    (format #t "~A~%" 3)))</enscript></td>
118<td><enscript highlight="c">FILE *out = fopen(path, "w");
119if (!out)
120  handle_error();
121fprintf(out, "%d\n", 1);
122fprintf(out, "%d\n", 2);
123fprintf(out, "%d\n", 3);
124if (!fclose(out))
125  handle_error();</enscript></td>
126<td>...</td>
127<td><enscript highlight="perl">
128open my $fh, '&gt;', $path;
129say $fh $_ foreach (1 .. 3);
130close $fh;
131</enscript></td></tr>
132</table>
133
134== Usage in a compiled program
135
136Some of the procedures listed here, e.g. {{(read-line)}} and {{(format ...)}}, are in the ''extras'' namespace, so if you are going to compile your program with {{csc}}, you'll need to add the following to your source file:
137
138 (use extras)
139
140== Lists
141
142== Vectors
143
144<table>
145<tr>
146<td>Declaring a vector of numbers</td>
147<td>An anonymous vector is declared as:
148
149<enscript highlight="scheme">`#(1 2 ,x ,y 5)</enscript>
150
151To declare it as a global variable:
152
153<enscript highlight="scheme">(define *my-vector* `#(1 2 ,x ,y 5))</enscript>
154
155Or, to declare it locally for some body of code:
156
157<enscript highlight="scheme">(let ((vector `#(1 2 ,x ,y 5)))
158  body-of-code ...)</enscript></td>
159<td>
160<enscript highlight="c">int vector[] = { 1, 2, x, y, 5 };</enscript>
161</td>
162</tr>
163
164<tr>
165<td>Obtaining the size of a vector</td>
166<td><enscript highlight="scheme">(vector-length my-vector)</enscript></td>
167<td>If the size of the vector is known at compile time:
168
169<enscript highlight="c">sizeof(my_vector)/sizeof(my_vector[0])</enscript>
170
171This often does not happen.  If the vector is terminated with a special element
172(such as a NULL pointer or —as in the case of strings— a 0), you'll have to traverse it:
173
174<enscript highlight="c">int i;
175for (i = 0; my_vector[i] != SPECIAL_ELEMENT; i ++);</enscript></td>
176</tr>
177
178<tr>
179<td>Printing a vector</td>
180<td>If you don't care that much about the format used:
181
182<enscript highlight="scheme">(format "~A~%" vector)</enscript>
183
184If you need a newline after each element and can afford the vector to be converted to a list:
185
186<enscript highlight="scheme">(use format-modular)
187(format "~{~A~%~}" (vector->list vector))</enscript>
188
189Without converting the vector to list:
190
191<enscript highlight="scheme">(do ((i 0 (+ i 1)))
192    ((= i (vector-length vector)))
193  (format #t "~A~%" (vector-ref vector i)))</enscript>
194</td>
195<td>
196<enscript highlight="c">int i;
197for (i = 0; i < n; i ++)
198  printf("%d\n", vector[i]);</enscript>
199</td>
200<td>...</td>
201</tr>
202
203</table>
204
205== Hash tables
206
207=== Creating a hash table
208
209==== CHICKEN
210
211<enscript highlight="scheme">(make-hash-table)</enscript>
212
213If you need to populate the hash table, you'll probably use {{alist->hash-table}}:
214<enscript highlight=scheme>(alist->hash-table '((1 . "uno") (2 . "dos")))</enscript>
215
216Note that the list can be created at runtime.
217
218==== C++ STL
219
220<enscript highlight="cpp">map<int, string> my_hash;
221my_hash[1] = "uno";
222my_hash[2] = "dos";</enscript>
223
224==== Perl
225
226<enscript highlight="perl">my %hash = (
227  1 => 'uno',
228  2 => 'dos'
229);</enscript>
230
231=== Obtaining an element from a hash
232
233==== CHICKEN
234
235<enscript highlight=scheme>(hash-table-ref/default my_hash 2 #f)</enscript>
236
237This will return {{#f}} (false) if the element is not defined.  If you prefer to have an error thrown, you should use:
238
239<enscript highlight=scheme>(hash-table-ref my_hash 2)</enscript>
240
241==== C++ STL
242
243<enscript highlight=cpp>my_hash[2]</enscript>
244
245==== Python
246
247my_hash[2]
248==== Perl
249
250<enscript highlight="perl">$my_hash{2}</enscript>
251
252=== Printing the keys in the hash
253
254==== CHICKEN
255
256If you can afford to have a list with the keys created, you should do this:
257
258<enscript highlight=scheme>(format #t "~{~A~%~}~%" (hash-table-keys my_hash))</enscript>
259
260Otherwise, do this:
261
262<enscript highlight=scheme>(hash-table-walk
263  my-hash
264  (lambda (key value)
265    (format #f "~A~%" key)))</enscript>
266
267==== C++ STL
268
269<enscript highlight="cpp">for(map<int, string>::iterator iter = my_hash.begin();
270    it != my_hash.end(); ++it )
271  cout << it->first << endl;</enscript>
272
273==== Python
274
275print my_hash.keys()
276==== Perl
277
278<enscript highlight=perl>
279say foreach keys %my_hash;
280</enscript>
281
282=== Getting the number of items in a hash
283
284==== CHICKEN
285
286<enscript highlight=scheme>(hash-table-size my-hash)</enscript>
287
288==== Python
289
290<enscript highlight=python>len(my-hash)</enscript>
Note: See TracBrowser for help on using the repository browser.