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

Last change on this file since 36866 was 36866, checked in by sjamaan, 20 months ago

Update file I/O section in CHICKEN for Python programmers to CHICKEN 5 (thanks to Sjors for pointing out the confusing old links)

File size: 4.2 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(import (chicken io))
114(call-with-input-file "my-file.txt" (lambda (port) (read-string #f port)))
115</enscript>
116
117{{read-string}} is defined in [[/man/5/Module (chicken io)|the (chicken io) module]]. {{call-with-input-file calls its argument procedure with the opened file as an argument. The first argument to {{read-string}} is the limit, which tells it how many characters to read. We use {{#f}} to indicate we want to read everything (no limit).
118
119=== Reading the contents of a file and returning a list of lines
120==== Python
121<enscript highlight=python>
122open("my-file.txt").readlines()
123</enscript>
124
125<enscript highlight=python>
126with open("my-file.txt") as f:
127    f.readlines()
128</enscript>
129
130==== CHICKEN
131<enscript highlight=scheme>
132(call-with-input-file "my-file.txt" (lambda (port) (read-lines port)))
133</enscript>
134
135{{read-lines}} is also defined in [[/man/5/Module (chicken io)|the (chicken io) module]].
136
137
138
139== Conditionals
140
141=== Single condition
142
143==== Python
144<enscript highlight=python>
145"yes" if 1<2 else "no"
146</enscript>
147
148==== CHICKEN
149<enscript highlight=scheme>
150(if (< 1 2) "yes" "no")
151</enscript>
152
153PS: in Scheme, the result of the evaluation of an {{if}} form is
154passed to its continuation.
155
156
157=== Multiple conditions
158
159==== Python
160<enscript highlight=python>
161result = None
162if 1 < 2:
163    result = "1 < 2"
164elif 1 > 2:
165    result = "1 > 2"
166else:
167    result = "1 = 2"
168</enscript>
169
170==== CHICKEN
171<enscript highlight=scheme>
172(cond ((< 1 2) "1 < 2")
173      ((> 1 2) "1 > 2")
174      (else "1 = 2"))
175</enscript>
176
177PS: in Scheme, the result of the evaluation of a {{cond}} form is
178passed to its continuation.
179
180
181== Iteration
182
183=== Iterating through and printing the elements of a list
184
185==== Python
186<enscript highlight=python>
187l = [1, 2, 3, 4]
188for i in l:
189    print i
190</enscript>
191
192==== CHICKEN
193<enscript highlight=scheme>
194(define l '(1 2 3 4))
195(for-each print l)
196</enscript>
197
198
199=== Applying a procedure/function to each item of a list
200
201==== Python
202<enscript highlight=python>
203l = [1, 2, 3, 4]
204
205def add10(n):
206    return n + 10
207
208[ add10(i) for i in l ]
209</enscript>
210
211
212==== CHICKEN
213<enscript highlight=scheme>
214(define l '(1 2 3 4))
215
216(define (add10 n) (+ n 10))
217
218(map add10 l)
219</enscript>
220
221
222== Other references
223
224Other interesting reads to check out:
225
226* Michele Simionato's blog series [[http://www.artima.com/weblogs/viewpost.jsp?thread=251474|"The Adventures of a Pythonista in Schemeland"]]
227* 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.