You are looking at historical revision 8612 of this page. It may differ significantly from its current revision.

Hello, world!

Python

print "Hello, world!"

Chicken

(print "Hello, world!")

Definitions, assignment and bindings

Python

Python has only one operator for both assignments and definitions (=).

a = 0

Chicken

Defining a variable:

(define a 0)

Assigning a variable a value:

(set! a 0)

PS: Chicken automatically defines a variable in the toplevel environment when a variable is set without being defined.

Binding a value to a variable:

(let ((a 0))
   a)

Strings

Concatenating

Python

a = "1" + "2"
a = ["1", "2"].join()

Chicken

(string-append "1" "2")
(conc "1" 2)
</enscript

<enscript highlight=scheme>
(string-intersperse '("1" "2") "")
</enscript

=== Spliting a string

==== Python
<enscript highlight=python>
"this is a string".split()
</enscript

==== Chicken
<enscript highlight=scheme>
(string-split "this is a string")
</enscript


== File I/O

=== Reading the contents of a file and returning a string
==== Python
<enscript highlight=python>
open("my-file.txt").read()
</enscript

==== Chicken
<enscript highlight=scheme>
(read-all "my-file.txt")
</enscript

{{read-all}} is defined in [[Unit utils]].

=== Reading the contents of a file and returning a list of lines
==== Python
<enscript highlight=python>
open("my-file.txt").readlines()

Chicken

(read-lines "my-file.txt")

read-lines is defined in Unit utils.

Conditionals

Single condition

Python

result = None
if 1 < 2:
    result = "yes"
else:
    result = "no"

Chicken

(if (< 1 2) "yes" "no")

PS: in Scheme, the result of the evaluation of an if form is passed to its continuation.

Multiple conditions

Python

result = None
if 1 < 2:
    result = "1 < 2"
elif 1 > 2:
    result = "1 > 2"
else:
    result = "1 = 2"

Chicken

(cond ((< 1 2) "1 < 2")
      ((> 1 2) "1 > 2")
      (else "1 = 2"))

PS: in Scheme, the result of the evaluation of a cond form is passed to its continuation.

Iteration

Iterating through and printing the elements of a list

Python

l = [1, 2, 3, 4]
for i in l:
    print i

Chicken

(define l '(1 2 3 4)) (for-each print l)

Applying a procedure/function to each item of a list

Python

l = [1, 2, 3, 4]

def add10(n):
    return n + 10

[ add10(i) for i in l ]

Chicken

(define l '(1 2 3 4))

(define (add10 n) (+ n 10))

(map add10 l)