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

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

Properly capitalize CHICKEN on the wiki directory (only first level).

I used the following shell script to change things:

while IFS= read -d $'\0' -r file ; do

sed -i 's/Chicken/CHICKEN/g' "$file"

done < <(find wiki -maxdepth 1 -type f -print0 )

Some files have been manually reverted after that, since some
substitutions don't apply:

  • friedly-chicken (repl banner)
  • survey2011 (Chicken in URI paths)
  • chickenista-guide (Chickenista)

I hope the link canonicalization thing will be on my side.

File size: 3.5 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==== CHICKEN
67<enscript highlight=scheme>
68(string-append "1" "2")
69</enscript>
70
71<enscript highlight=scheme>
72(conc "1" "2")
73</enscript>
74
75<enscript highlight=scheme>
76(string-intersperse '("1" "2") "")
77</enscript>
78
79=== Splitting a string
80
81==== Python
82<enscript highlight=python>
83"this is a string".split()
84</enscript>
85
86==== CHICKEN
87<enscript highlight=scheme>
88(string-split "this is a string")
89</enscript>
90
91
92== File I/O
93
94=== Reading the contents of a file and returning a string
95==== Python
96<enscript highlight=python>
97open("my-file.txt").read()
98</enscript>
99
100==== CHICKEN
101<enscript highlight=scheme>
102(read-all "my-file.txt")
103</enscript>
104
105{{read-all}} is defined in [[/manual/Unit utils|Unit utils]].
106
107=== Reading the contents of a file and returning a list of lines
108==== Python
109<enscript highlight=python>
110open("my-file.txt").readlines()
111</enscript>
112
113==== CHICKEN
114<enscript highlight=scheme>
115(read-lines "my-file.txt")
116</enscript>
117
118{{read-lines}} is defined in [[/manual/Unit utils|Unit utils]].
119
120
121
122== Conditionals
123
124=== Single condition
125
126==== Python
127<enscript highlight=python>
128"yes" if 1<2 else "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=scheme>
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://plr.sourceforge.net/cgi-bin/plr/launch.py|language comparison table]] ("Pointless Programming Reference")
Note: See TracBrowser for help on using the repository browser.