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

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

Another page for the "chicken for <other programming language> programmers" series. Now it's Python.

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