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

Last change on this file since 36409 was 32554, checked in by svnwiki, 5 years ago

Anonymous wiki edit for IP [127.0.0.1]: Renewing

File size: 3.8 KB
Line 
1== CHICKEN for Python programmers
2
3[[toc:]]
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 of variables ({{=}}).
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 global variable
42when a variable is set without being defined.  However, it is
43best practice (and most compatible with other Schemes) to define
44all variables.
45
46Binding a value to a variable:
47
48<enscript highlight=scheme>
49(let ((a 0))
50   a)
51</enscript>
52
53== Strings
54
55=== Concatenating
56
57==== Python
58<enscript highlight=python>
59a = "1" + "2"
60</enscript>
61
62<enscript highlight=python>
63a = "".join(["1", "2"])
64</enscript>
65
66
67<enscript highlight=python>
68a = sum(["1", "2"], start="")
69</enscript>
70
71==== CHICKEN
72<enscript highlight=scheme>
73(string-append "1" "2")
74</enscript>
75
76<enscript highlight=scheme>
77(conc "1" "2")
78</enscript>
79
80<enscript highlight=scheme>
81(string-intersperse '("1" "2") "")
82</enscript>
83
84=== Splitting a string
85
86==== Python
87<enscript highlight=python>
88"this is a string".split()
89</enscript>
90
91==== CHICKEN
92<enscript highlight=scheme>
93(string-split "this is a string")
94</enscript>
95
96
97== File I/O
98
99=== Reading the contents of a file and returning a string
100==== Python
101<enscript highlight=python>
102open("my-file.txt").read() # Deprecated
103</enscript>
104
105<enscript highlight=python>
106with open("my-file.txt") as f:
107    f.read()
108</enscript>
109
110
111==== CHICKEN
112<enscript highlight=scheme>
113(read-all "my-file.txt")
114</enscript>
115
116{{read-all}} is defined in [[/manual/Unit utils|Unit utils]].
117
118=== Reading the contents of a file and returning a list of lines
119==== Python
120<enscript highlight=python>
121open("my-file.txt").readlines()
122</enscript>
123
124<enscript highlight=python>
125with open("my-file.txt") as f:
126    f.readlines()
127</enscript>
128
129==== CHICKEN
130<enscript highlight=scheme>
131(read-lines "my-file.txt")
132</enscript>
133
134{{read-lines}} is defined in [[/manual/Unit utils|Unit utils]].
135
136
137
138== Conditionals
139
140=== Single condition
141
142==== Python
143<enscript highlight=python>
144"yes" if 1<2 else "no"
145</enscript>
146
147==== CHICKEN
148<enscript highlight=scheme>
149(if (< 1 2) "yes" "no")
150</enscript>
151
152PS: in Scheme, the result of the evaluation of an {{if}} form is
153passed to its continuation.
154
155
156=== Multiple conditions
157
158==== Python
159<enscript highlight=python>
160result = None
161if 1 < 2:
162    result = "1 < 2"
163elif 1 > 2:
164    result = "1 > 2"
165else:
166    result = "1 = 2"
167</enscript>
168
169==== CHICKEN
170<enscript highlight=scheme>
171(cond ((< 1 2) "1 < 2")
172      ((> 1 2) "1 > 2")
173      (else "1 = 2"))
174</enscript>
175
176PS: in Scheme, the result of the evaluation of a {{cond}} form is
177passed to its continuation.
178
179
180== Iteration
181
182=== Iterating through and printing the elements of a list
183
184==== Python
185<enscript highlight=python>
186l = [1, 2, 3, 4]
187for i in l:
188    print i
189</enscript>
190
191==== CHICKEN
192<enscript highlight=scheme>
193(define l '(1 2 3 4))
194(for-each print l)
195</enscript>
196
197
198=== Applying a procedure/function to each item of a list
199
200==== Python
201<enscript highlight=python>
202l = [1, 2, 3, 4]
203
204def add10(n):
205    return n + 10
206
207[ add10(i) for i in l ]
208</enscript>
209
210
211==== CHICKEN
212<enscript highlight=scheme>
213(define l '(1 2 3 4))
214
215(define (add10 n) (+ n 10))
216
217(map add10 l)
218</enscript>
219
220
221== Other references
222
223Other interesting reads to check out:
224
225* Michele Simionato's blog series [[http://www.artima.com/weblogs/viewpost.jsp?thread=251474|"The Adventures of a Pythonista in Schemeland"]]
226* Jack Trades' [[http://plr.sourceforge.net/cgi-bin/plr/launch.py|language comparison table]] ("Pointless Programming Reference")
Note: See TracBrowser for help on using the repository browser.