You are looking at historical revision 27913 of this page. It may differ significantly from its current revision.
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.
Where PHP use the general syntax f(args, ...); , scheme use (f args...)
substr("abcdef", 0, 2);
is in Scheme:
(substring "abcdef" 0 2)
You will note that:
- All the expression is enclosed in the parenthesis, not just the arguments
- Arguments are separated by spaces, not by commas
- The expression is closes by the final closing parenthesis so there is no need for a ;
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))
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"))
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.
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)
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") ";")
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:
Doing several string replacements at once
str_replace(array('PHP', 'Python'), array('Scheme', 'Clojure'), 'PHP and Python are nice programming languages')
(string-translate* "PHP and Python are nice programming languages" '(("PHP" . "Scheme") ("Python" . "Clojure")))
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).