source: project/wiki/language-comparison @ 8585

Last change on this file since 8585 was 8585, checked in by azul, 12 years ago

Changes applied for azul (84.75.142.208) through svnwiki:

Creating...

File size: 5.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</tr>
11
12<tr>
13<td>Formatting and printing to stdout</td>
14<td><enscript highlight="scheme">(format #t "I want ~A ~A~%" count desc)</enscript></td>
15<td>printf("I want %d %s\n", count, desc);</td>
16<td>...</td>
17</tr>
18
19<tr>
20<td>Printing to stderr</td>
21<td><enscript highlight="scheme">(format (current-error-port) "Ooops~%")</enscript></td>
22<td>fprintf(stderr, "Ooops\n");</td>
23<td>...</td>
24</tr>
25
26<tr>
27<td>Reading a line from stdin</td>
28<td><enscript highlight="scheme">(read-line)</enscript></td>
29<td><enscript highlight="c">char buffer[1024];
30fgets(buffer, sizeof(buffer)/sizeof(buffer[0], stdin);</enscript></td>
31<td>...</td>
32</tr>
33
34
35<tr>
36<td>Opening a file for input</td>
37<td><enscript highlight="scheme">(open-input-file path)</enscript></td>
38<td><enscript highlight="c">fopen(path, "r");</enscript></td>
39<td>...</td>
40</tr>
41
42
43<tr>
44<td>Opening a file for output</td>
45<td><enscript highlight="scheme">(open-output-file path)</enscript></td>
46<td><enscript highlight="c">fopen(path, "w");</enscript></td>
47<td>...</td>
48</tr>
49
50
51<tr>
52<td>Count lines in a file</td>
53<td><enscript highlight="scheme">(with-input-from-file path
54  (lambda ()
55    (let loop ((count 0))
56      (if (eof-object? (read-line))
57        count
58        (loop (+ count 1)))))))</enscript>
59
60If you don't want to keep an entire line in memory at any time:
61
62<enscript highlight="scheme">(with-input-from-file path
63  (lambda ()
64    (let loop ((count 0) (c (read-char)))
65      (if (eof-object? c)
66        count
67        (loop (if (char=? c #\newline) (+ count 1) count) (read-char))))))</enscript>
68</td>
69<td><enscript highlight="c">FILE *in = fopen(path, "r");
70int c, count = 0;
71if (!in)
72  handle_error();
73while ((c = fgetc(in)) != EOF)
74  if (c == '\n')
75    lines ++;
76fclose(in);
77return count;</enscript></td>
78<td>...</td>
79</tr>
80
81<tr>
82<td>Write a file with some numbers
83
84In practice you'd probably collapse the calls to format and equivalent functions in a single call.
85</td>
86<td><enscript highlight="scheme">(with-output-to-file path
87  (lambda ()
88    (format #t "~A~%" 1)
89    (format #t "~A~%" 2)
90    (format #t "~A~%" 3)))</enscript></td>
91<td><enscript highlight="c">FILE *out = fopen(path, "w");
92if (!out)
93  handle_error();
94fprintf(out, "%d\n", 1);
95fprintf(out, "%d\n", 2);
96fprintf(out, "%d\n", 3);
97if (!fclose(out))
98  handle_error();</enscript></td></tr>
99</table>
100
101== Lists
102
103== Vectors
104
105<table>
106<tr>
107<td>Declaring a vector of numbers</td>
108<td>An anonymous vector is declared as:
109
110<enscript highlight="scheme">`#(1 2 ,x ,y 5)</enscript>
111
112To declare it as a global variable:
113
114<enscript highlight="scheme">(define *my-vector* `#(1 2 ,x ,y 5))</enscript>
115
116Or, to declare it locally for some body of code:
117
118<enscript highlight="scheme">(let ((vector `#(1 2 ,x ,y 5)))
119  body-of-code ...)</enscript></td>
120<td>
121<enscript highlight="c">int vector[] = { 1, 2, x, y, 5 };</enscript>
122</td>
123</tr>
124
125<tr>
126<td>Obtaining the size of a vector</td>
127<td><enscript highlight="scheme">(vector-length my-vector)</enscript></td>
128<td>If the size of the vector is known at compile time:
129
130<enscript highlight="c">sizeof(my_vector)/sizeof(my_vector[0])</enscript>
131
132This often does not happen.  If the vector is terminated with a special element
133(such as a NULL pointer or —as in the case of strings— a 0), you'll have to traverse it:
134
135<enscript highlight="c">int i;
136for (i = 0; my_vector[i] != SPECIAL_ELEMENT; i ++);</enscript></td>
137</tr>
138
139<tr>
140<td>Printing a vector</td>
141<td>If you don't care that much about the format used:
142
143<enscript highlight="scheme">(format "~A~%" vector)</enscript>
144
145If you need a newline after each element and can afford the vector to be converted to a list:
146
147<enscript highlight="scheme">(use format-modular)
148(format "~{~A~%~}" (vector->list vector))</enscript>
149
150Without converting the vector to list:
151
152<enscript highlight="scheme">(do ((i 0 (+ i 1)))
153    ((= i (vector-length vector)))
154  (format #t "~A~%" (vector-ref vector i)))</enscript>
155</td>
156<td>
157<enscript highlight="c">int i;
158for (i = 0; i < n; i ++)
159  printf("%d\n", vector[i]);</enscript>
160</td>
161<td>...</td>
162</tr>
163
164</table>
165
166== Hash tables
167
168<table>
169<tr>
170<td></td>
171<td>Chicken</td>
172<td>C++ (STL)</td>
173<td>Python</td>
174<td>Perl</td>
175</tr>
176
177<tr>
178<td>Creating a hash table</td>
179<td><enscript highlight="scheme">(make-hash-table)</enscript>
180
181If you need to populate the hash table, you'll probably use {{alist->hash-table}}:
182<enscript highlight=scheme>(alist->hash-table '((1 . "uno") (2 . "dos")))</enscript>
183
184Note that the list can be created at runtime.</td>
185<td><enscript highlight="cpp">map<int, string> my_hash;
186my_hash[1] = "uno";
187my_hash[2] = "dos";</enscript></td>
188<td></td>
189<td>
190<enscript highlight="perl">my %hash = (
191  1 => 'uno',
192  2 => 'dos'
193);</enscript></td>
194</tr>
195
196<tr>
197<td>Obtaining an element from a hash</td>
198<td><enscript highlight=scheme>(hash-table-ref/default my_hash 2 #f)</enscript>
199
200This will return #f (false) if the element is not defined.  If you prefer to have an error thrown, you should use:
201
202<enscript highlight=scheme>(hash-table-ref my_hash 2)</enscript></td>
203<td>my_hash[2]</td>
204<td></td>
205<td><enscript highlight="perl">$my_hash{2}</enscript></td>
206</tr>
207
208<tr>
209<td>Printing the keys in the hash</td>
210<td>If you can afford to have a list with the keys created, you should do this:
211<enscript highlight=scheme>(format #t "~{~A~}~%" (hash-table-keys my_hash))</enscript>
212
213Otherwise, do this:
214
215<enscript highlight=scheme>(hash-table-walk
216  my-hash
217  (lambda (key value)
218    (format #f "~A~%" key)))</enscript></td>
219<td><enscript highlight="cpp">for(map<int, string>::iterator iter = my_hash.begin();
220    it != my_hash.end(); it++ )
221  cout << it->first << endl;</enscript>
222</td>
223<td></td>
224<td><enscript highlight=perl>for my $key ( keys %my_hash ) {
225  print "$key\n";
226}</enscript></td></tr>
227</table>
Note: See TracBrowser for help on using the repository browser.