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

Last change on this file since 15317 was 15317, checked in by svnwiki, 11 years ago

Changes applied for John Cowan (72.14.228.129) through svnwiki:

Added recommendation to define all variables.

File size: 3.5 KB
Line 
1[[toc:]]
2
3== Hello, world!
4
5=== Python
6<enscript highlight=python>
7print "Hello, world!"
8</enscript>
9
10=== Chicken
11<enscript highlight=scheme>
12(print "Hello, world!")
13</enscript>
14
15== Definitions, assignment and bindings
16
17=== Python
18
19Python has only one operator for both assignments and definitions of variables ({{=}}).
20
21<enscript highlight=python>
22a = 0
23</enscript>
24
25=== Chicken
26
27Defining a variable:
28
29<enscript highlight=scheme>
30(define a 0)
31</enscript>
32
33Assigning a variable a value:
34
35<enscript highlight=scheme>
36(set! a 0)
37</enscript>
38
39PS: Chicken automatically defines a global variable
40when a variable is set without being defined.  However, it is
41best practice (and most compatible with other Schemes) to define
42all variables.
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 = "".join(["1", "2"])
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=== Splitting 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 [[/manual/Unit utils|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 [[/manual/Unit utils|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<enscript highlight=python>
179(define l '(1 2 3 4))
180(for-each print l)
181</enscript>
182
183
184=== Applying a procedure/function to each item of a list
185
186==== Python
187<enscript highlight=python>
188l = [1, 2, 3, 4]
189
190def add10(n):
191    return n + 10
192
193[ add10(i) for i in l ]
194</enscript>
195
196
197==== Chicken
198<enscript highlight=scheme>
199(define l '(1 2 3 4))
200
201(define (add10 n) (+ n 10))
202
203(map add10 l)
204</enscript>
205
206
207== Other references
208
209Other interesting reads to check out:
210
211* Michele Simionato's blog series [[http://www.artima.com/weblogs/viewpost.jsp?thread=251474|"The Adventures of a Pythonista in Schemeland"]]
212* Jack Trades' [[http://plr.sourceforge.net/python-to-chicken.html|Chicken for Python Programmers]]
Note: See TracBrowser for help on using the repository browser.