1. Syntax
  2. Variables
    1. Booleans
    2. Numbers
    3. Strings
      1. Exploding and imploding
      2. Doing several string replacements at once
        1. PHP
        2. CHICKEN
    4. Data structures

The following page is an introduction to CHICKEN intended to PHP programmers.

This is a work in progress, I will try to work on as frequently as possible (mostly on sunday). In the meantime, feel free to help me by expanding it, correcting (english is not my native language) and/or by commenting on what I have done so far.

CHICKEN is an implementation of the Scheme programming language which, in turn is a member of the Lisp family of languages. More information about this on the http://schemers.org/ website.

Syntax

Where PHP use the general syntax f(args, ...); , scheme use (f args...)

For example:

substr("abcdef", 0, 2);

is in Scheme:

(substring "abcdef" 0 2)

You will note that:

All scheme expressions use this format, including arithmetic.

The php expression:

3 + 5

Is in scheme:

(+ 3 5)

This is called prefix notation. It may take some time to get used to it but it had several advantages; mostly by avoiding any ambiguity in the operator precedences.

A more complex example can be:

3 + 5 - 12 * 2

Is represented in scheme as:

(- (+ 3 5)(* 12 2))

Variables

Like PHP, scheme does not type variables. If you assign $var to a string, it is a string. You re-assign it to 3, it become a number.

However, it should be noted that Scheme is much stricter when dealing with types. In PHP, you can usually omit typecasts, intermixing data of different types:

10 + "5"

Is in Scheme it would cause an error, and you have to expicitly convert a string to a number:

(+ 10 (string->number "5"))

Booleans

TRUE, in Scheme, is noted #t while FALSE is noted #f. Unlike PHP, all Scheme values are considered to be true (#t) except #f itself, including zero (0), the empty string and others empty structures.

(if (- 2 2) "hello" "goodbye")
;; "hello"

Numbers

All type of numbers are supported by CHICKEN, including real, complex and rational, natively (without resorting to external libraries).

Number can be expressed in binary, octal, decimal or hexadecimal notation by using the prefixs #b for binary, #o for octal, #d for decimal and #x for hexadecimal. Unprefixed number are deicmal by default (so 21 and #d21 are the same)

PHP scheme
21 #d21
025 #o25
0x15 #x15
n/a #b10101

Strings

Scheme support a vast array of operations on string, including changing case, splitting, extracting substring and regular expressions based searching and replacing

Exploding and imploding

implode(';', array('a', 'b', 'c', 'd'))

is in CHICKEN Scheme:

(string-intersperse '("a" "b" "c" "d") ";")
explode(';', 'a;b;c;d')

is in CHICKEN Scheme:

(string-split "a;b;c;d" ";")

Please not that the functions string-intersperse and string-split are in the extras unit, so if you are compiling a program, you will need to add this in the beginning of your code:

(use extras)

Doing several string replacements at once

PHP
str_replace(array('PHP', 'Python'), array('Scheme', 'Clojure'), 'PHP and Python are nice programming languages')
CHICKEN
(string-translate* "PHP and Python are nice programming languages" '(("PHP" . "Scheme") ("Python" . "Clojure")))

Data structures

The base data structure of PHP is the array. In Scheme it is the list.

However, lists in Scheme are not as versatile as PHP arrays: lists in Scheme are best used for data that is accessed sequentially. So Scheme provides several other data types: vectors (arrays in a usual sense, allowing with a random access by an integer number) and hashtables (allow to access elements by any key).