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

Last change on this file since 14110 was 14110, checked in by sjamaan, 12 years ago

Fix most direct links to the manual sections

File size: 3.4 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.
41
42Binding a value to a variable:
43
44<enscript highlight=scheme>
45(let ((a 0))
46   a)
47</enscript>
48
49== Strings
50
51=== Concatenating
52
53==== Python
54<enscript highlight=python>
55a = "1" + "2"
56</enscript>
57
58<enscript highlight=python>
59a = "".join(["1", "2"])
60</enscript>
61
62==== Chicken
63<enscript highlight=scheme>
64(string-append "1" "2")
65</enscript>
66
67<enscript highlight=scheme>
68(conc "1" 2)
69</enscript>
70
71<enscript highlight=scheme>
72(string-intersperse '("1" "2") "")
73</enscript>
74
75=== Spliting a string
76
77==== Python
78<enscript highlight=python>
79"this is a string".split()
80</enscript>
81
82==== Chicken
83<enscript highlight=scheme>
84(string-split "this is a string")
85</enscript>
86
87
88== File I/O
89
90=== Reading the contents of a file and returning a string
91==== Python
92<enscript highlight=python>
93open("my-file.txt").read()
94</enscript>
95
96==== Chicken
97<enscript highlight=scheme>
98(read-all "my-file.txt")
99</enscript>
100
101{{read-all}} is defined in [[/manual/Unit utils|Unit utils]].
102
103=== Reading the contents of a file and returning a list of lines
104==== Python
105<enscript highlight=python>
106open("my-file.txt").readlines()
107</enscript>
108
109==== Chicken
110<enscript highlight=scheme>
111(read-lines "my-file.txt")
112</enscript>
113
114{{read-lines}} is defined in [[/manual/Unit utils|Unit utils]].
115
116
117
118== Conditionals
119
120=== Single condition
121
122==== Python
123<enscript highlight=python>
124result = None
125if 1 < 2:
126    result = "yes"
127else:
128    result = "no"
129</enscript>
130
131==== Chicken
132<enscript highlight=scheme>
133(if (< 1 2) "yes" "no")
134</enscript>
135
136PS: in Scheme, the result of the evaluation of an {{if}} form is
137passed to its continuation.
138
139
140=== Multiple conditions
141
142==== Python
143<enscript highlight=python>
144result = None
145if 1 < 2:
146    result = "1 < 2"
147elif 1 > 2:
148    result = "1 > 2"
149else:
150    result = "1 = 2"
151</enscript>
152
153==== Chicken
154<enscript highlight=scheme>
155(cond ((< 1 2) "1 < 2")
156      ((> 1 2) "1 > 2")
157      (else "1 = 2"))
158</enscript>
159
160PS: in Scheme, the result of the evaluation of a {{cond}} form is
161passed to its continuation.
162
163
164== Iteration
165
166=== Iterating through and printing the elements of a list
167
168==== Python
169<enscript highlight=python>
170l = [1, 2, 3, 4]
171for i in l:
172    print i
173</enscript>
174
175==== Chicken
176<enscript highlight=python>
177(define l '(1 2 3 4))
178(for-each print l)
179</enscript>
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>
203
204
205== Other references
206
207Other interesting reads to check out:
208
209* Michele Simionato's blog series [[http://www.artima.com/weblogs/viewpost.jsp?thread=251474|"The Adventures of a Pythonista in Schemeland"]]
210* Jack Trades' [[http://docs.google.com/Doc?id=dgxzmhv_579j3m5kc5|Python to Chicken Language Translator]]
Note: See TracBrowser for help on using the repository browser.