source: project/wiki/chicken-for-python-programmers @ 8615

Last change on this file since 8615 was 8615, checked in by Mario Domenech Goulart, 12 years ago

More syntax fixes.

File size: 3.0 KB
Line 
1== Hello, world!
2
3==== Python
4<enscript highlight=python>
5print "Hello, world!"
6</enscript>
7
8==== Chicken
9<enscript highlight=scheme>
10(print "Hello, world!")
11</enscript>
12
13== Definitions, assignment and bindings
14
15==== Python
16
17Python has only one operator for both assignments and definitions ({{=}}).
18
19<enscript highlight=python>
20a = 0
21</enscript>
22
23==== Chicken
24
25Defining a variable:
26
27<enscript highlight=scheme>
28(define a 0)
29</enscript>
30
31Assigning a variable a value:
32
33<enscript highlight=scheme>
34(set! a 0)
35</enscript>
36
37PS: Chicken automatically defines a variable in the toplevel
38environment when a variable is set without being defined.
39
40Binding a value to a variable:
41
42<enscript highlight=scheme>
43(let ((a 0))
44   a)
45</enscript>
46
47== Strings
48
49=== Concatenating
50
51==== Python
52<enscript highlight=python>
53a = "1" + "2"
54</enscript>
55
56<enscript highlight=python>
57a = ["1", "2"].join()
58</enscript>
59
60==== Chicken
61<enscript highlight=scheme>
62(string-append "1" "2")
63</enscript>
64
65<enscript highlight=scheme>
66(conc "1" 2)
67</enscript>
68
69<enscript highlight=scheme>
70(string-intersperse '("1" "2") "")
71</enscript>
72
73=== Spliting a string
74
75==== Python
76<enscript highlight=python>
77"this is a string".split()
78</enscript>
79
80==== Chicken
81<enscript highlight=scheme>
82(string-split "this is a string")
83</enscript>
84
85
86== File I/O
87
88=== Reading the contents of a file and returning a string
89==== Python
90<enscript highlight=python>
91open("my-file.txt").read()
92</enscript>
93
94==== Chicken
95<enscript highlight=scheme>
96(read-all "my-file.txt")
97</enscript>
98
99{{read-all}} is defined in [[Unit utils]].
100
101=== Reading the contents of a file and returning a list of lines
102==== Python
103<enscript highlight=python>
104open("my-file.txt").readlines()
105</enscript>
106
107==== Chicken
108<enscript highlight=scheme>
109(read-lines "my-file.txt")
110</enscript>
111
112{{read-lines}} is defined in [[Unit utils]].
113
114
115
116== Conditionals
117
118=== Single condition
119
120==== Python
121<enscript highlight=python>
122result = None
123if 1 < 2:
124    result = "yes"
125else:
126    result = "no"
127</enscript>
128
129==== Chicken
130<enscript highlight=scheme>
131(if (< 1 2) "yes" "no")
132</enscript>
133
134PS: in Scheme, the result of the evaluation of an {{if}} form is
135passed to its continuation.
136
137
138=== Multiple conditions
139
140==== Python
141<enscript highlight=python>
142result = None
143if 1 < 2:
144    result = "1 < 2"
145elif 1 > 2:
146    result = "1 > 2"
147else:
148    result = "1 = 2"
149</enscript>
150
151==== Chicken
152<enscript highlight=scheme>
153(cond ((< 1 2) "1 < 2")
154      ((> 1 2) "1 > 2")
155      (else "1 = 2"))
156</enscript>
157
158PS: in Scheme, the result of the evaluation of a {{cond}} form is
159passed to its continuation.
160
161
162== Iteration
163
164=== Iterating through and printing the elements of a list
165
166==== Python
167<enscript highlight=python>
168l = [1, 2, 3, 4]
169for i in l:
170    print i
171</enscript>
172
173==== Chicken
174(define l '(1 2 3 4))
175(for-each print l)
176
177
178=== Applying a procedure/function to each item of a list
179
180==== Python
181<enscript highlight=python>
182l = [1, 2, 3, 4]
183
184def add10(n):
185    return n + 10
186
187[ add10(i) for i in l ]
188</enscript>
189
190
191==== Chicken
192<enscript highlight=scheme>
193(define l '(1 2 3 4))
194
195(define (add10 n) (+ n 10))
196
197(map add10 l)
198</enscript>
Note: See TracBrowser for help on using the repository browser.