source: project/wiki/language-comparison @ 29353

Last change on this file since 29353 was 29353, checked in by svnwiki, 7 years ago

Anonymous wiki edit for IP [85.164.10.19]: sizeof(char) is defined to be 1 in C, so no need to divide sizeof(buffer) by sizeof(buffer[0])

File size: 6.7 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
66If you don't want to keep an entire line in memory at any time:
67
68<enscript highlight="scheme">(with-input-from-file path
69  (lambda ()
70    (let loop ((count 0) (c (read-char)))
71      (if (eof-object? c)
72        count
73        (loop (if (char=? c #\newline) (+ count 1) count) (read-char))))))</enscript>
74</td>
75<td><enscript highlight="c">FILE *in = fopen(path, "r");
76int c, count = 0;
77if (!in)
78  handle_error();
79while ((c = fgetc(in)) != EOF)
80  if (c == '\n')
81    lines ++;
82fclose(in);
83return count;</enscript></td>
84<td>...</td>
85<td><enscript highlight="perl">
86open my $fh, '&lt;', $path;
87my $count = &gt;$fh&lt;;
88close $fh;
89</enscript</td>
90</tr>
91
92<tr>
93<td>Write a file with some numbers
94
95In practice you'd probably collapse the calls to format and equivalent functions in a single call.
96</td>
97<td><enscript highlight="scheme">(with-output-to-file path
98  (lambda ()
99    (format #t "~A~%" 1)
100    (format #t "~A~%" 2)
101    (format #t "~A~%" 3)))</enscript></td>
102<td><enscript highlight="c">FILE *out = fopen(path, "w");
103if (!out)
104  handle_error();
105fprintf(out, "%d\n", 1);
106fprintf(out, "%d\n", 2);
107fprintf(out, "%d\n", 3);
108if (!fclose(out))
109  handle_error();</enscript></td>
110<td>...</td>
111<td><enscript highlight="perl">
112open my $fh, '&gt;', $path;
113say $fh $_ foreach (1 .. 3);
114close $fh;
115</enscript></td></tr>
116</table>
117
118== Usage in a compiled program
119
120Some 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:
121
122 (use extras)
123
124== Lists
125
126== Vectors
127
128<table>
129<tr>
130<td>Declaring a vector of numbers</td>
131<td>An anonymous vector is declared as:
132
133<enscript highlight="scheme">`#(1 2 ,x ,y 5)</enscript>
134
135To declare it as a global variable:
136
137<enscript highlight="scheme">(define *my-vector* `#(1 2 ,x ,y 5))</enscript>
138
139Or, to declare it locally for some body of code:
140
141<enscript highlight="scheme">(let ((vector `#(1 2 ,x ,y 5)))
142  body-of-code ...)</enscript></td>
143<td>
144<enscript highlight="c">int vector[] = { 1, 2, x, y, 5 };</enscript>
145</td>
146</tr>
147
148<tr>
149<td>Obtaining the size of a vector</td>
150<td><enscript highlight="scheme">(vector-length my-vector)</enscript></td>
151<td>If the size of the vector is known at compile time:
152
153<enscript highlight="c">sizeof(my_vector)/sizeof(my_vector[0])</enscript>
154
155This often does not happen.  If the vector is terminated with a special element
156(such as a NULL pointer or —as in the case of strings— a 0), you'll have to traverse it:
157
158<enscript highlight="c">int i;
159for (i = 0; my_vector[i] != SPECIAL_ELEMENT; i ++);</enscript></td>
160</tr>
161
162<tr>
163<td>Printing a vector</td>
164<td>If you don't care that much about the format used:
165
166<enscript highlight="scheme">(format "~A~%" vector)</enscript>
167
168If you need a newline after each element and can afford the vector to be converted to a list:
169
170<enscript highlight="scheme">(use format-modular)
171(format "~{~A~%~}" (vector->list vector))</enscript>
172
173Without converting the vector to list:
174
175<enscript highlight="scheme">(do ((i 0 (+ i 1)))
176    ((= i (vector-length vector)))
177  (format #t "~A~%" (vector-ref vector i)))</enscript>
178</td>
179<td>
180<enscript highlight="c">int i;
181for (i = 0; i < n; i ++)
182  printf("%d\n", vector[i]);</enscript>
183</td>
184<td>...</td>
185</tr>
186
187</table>
188
189== Hash tables
190
191=== Creating a hash table
192
193==== Chicken
194
195<enscript highlight="scheme">(make-hash-table)</enscript>
196
197If you need to populate the hash table, you'll probably use {{alist->hash-table}}:
198<enscript highlight=scheme>(alist->hash-table '((1 . "uno") (2 . "dos")))</enscript>
199
200Note that the list can be created at runtime.
201
202==== C++ STL
203
204<enscript highlight="cpp">map<int, string> my_hash;
205my_hash[1] = "uno";
206my_hash[2] = "dos";</enscript>
207
208==== Perl
209
210<enscript highlight="perl">my %hash = (
211  1 => 'uno',
212  2 => 'dos'
213);</enscript>
214
215=== Obtaining an element from a hash
216
217==== Chicken
218
219<enscript highlight=scheme>(hash-table-ref/default my_hash 2 #f)</enscript>
220
221This will return {{#f}} (false) if the element is not defined.  If you prefer to have an error thrown, you should use:
222
223<enscript highlight=scheme>(hash-table-ref my_hash 2)</enscript>
224
225==== C++ STL
226
227<enscript highlight=cpp>my_hash[2]</enscript>
228
229==== Python
230
231my_hash[2]
232==== Perl
233
234<enscript highlight="perl">$my_hash{2}</enscript>
235
236=== Printing the keys in the hash
237
238==== Chicken
239
240If you can afford to have a list with the keys created, you should do this:
241
242<enscript highlight=scheme>(format #t "~{~A~%~}~%" (hash-table-keys my_hash))</enscript>
243
244Otherwise, do this:
245
246<enscript highlight=scheme>(hash-table-walk
247  my-hash
248  (lambda (key value)
249    (format #f "~A~%" key)))</enscript>
250
251==== C++ STL
252
253<enscript highlight="cpp">for(map<int, string>::iterator iter = my_hash.begin();
254    it != my_hash.end(); ++it )
255  cout << it->first << endl;</enscript>
256
257==== Python
258
259print my_hash.keys()
260==== Perl
261
262<enscript highlight=perl>
263say foreach keys %my_hash;
264</enscript>
265
266=== Getting the number of items in a hash
267
268==== Chicken
269
270<enscript highlight=scheme>(hash-table-size my-hash)</enscript>
271
272==== Python
273
274<enscript highlight=python>len(my-hash)</enscript>
Note: See TracBrowser for help on using the repository browser.