You are looking at historical revision 26758 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.

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.

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

In PHP, str_replace function can take arrays of strings as its argument to do several replacements at once. Scheme doesn't have such a possibility by default, but you can easily make such a function yourself. Here is an example to make a function with such functionality:

;;Put this near the beginning of your file
(require 'srfi-1 'pregexp)

(define (pregexp-replace*-many haystack patterns replacements)
  (fold (lambda (pattern replacement current-haystack)
                (pregexp-replace* pattern current-haystack replacement))
          haystack
          patterns
          replacements))

However, pregexp-replace*-many is based on pregexp-replace*, so it accepts a list of regular expressions, not list of strings. To fully emulate PHP behaviour and make it accept strings, we can use the pregexp-quote function that takes a string and returns a regular expression pattern (we can either modify the previous function, or make a function that modifies arguments and calls pregexp-replace*-many):

(define (string-replace*-many haystack needles replacements)
  (fold (lambda (needle replacement current-haystack)
                (pregexp-replace* (pregexp-quote needle) current-haystack replacement))
          haystack 
          needles 
          replacements))

;;Alternative implementation
(define (string-replace*-many haystack needles replacements)
  (pregexp-replace*-many
    haystack
    (map (lambda (x) (pregexp-quote x)) needles)
    replacements))

So, if you define these functions, the equivalent of the PHP following code:

str_replace('PHP and Python are nice programming languages', array('PHP', 'Python'), array('Scheme', 'Clojure'))

...would look like this:

(string-replace*-many "PHP and Python are nice programming languages"
                      '("PHP" "Python") '("Scheme" "Clojure"))

As you can see, Scheme doesn't provide a ready-to-use function, but it provides a function fold that allows to creare your own functions that take arrays as arguments. The upside is that you can use fold to create any kinds of functions, not just for string replacement.

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).