source: project/wiki/chicken-for-php-programmers @ 38069

Last change on this file since 38069 was 36915, checked in by sjamaan, 16 months ago

Updates examples in CHICKEN for PHP programmers to CHICKEN 5

File size: 4.6 KB
Line 
1[[toc:]]
2
3The following page is an introduction to CHICKEN intended to PHP programmers.
4
5This 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.
6
7CHICKEN 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/|http://schemers.org/]] website.
8
9== Syntax
10
11Where PHP use the general syntax f(args, ...); , scheme use (f args...)
12
13For example:
14
15<enscript highlight=c>
16substr("abcdef", 0, 2);
17</enscript>
18is in Scheme:
19<enscript highlight=scheme>
20(substring "abcdef" 0 2)
21</enscript>
22
23You will note that:
24* All the expression is enclosed in the parenthesis, not just the arguments
25* Arguments are separated by spaces, not by commas
26* The expression is closed by the final closing parenthesis so there is no need for a ;
27
28All scheme expressions use this format, including arithmetic.
29
30The php expression:
31<enscript highlight=c>
323 + 5
33</enscript>
34Is in scheme:
35<enscript highlight=scheme>
36(+ 3 5)
37</enscript>
38
39This 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.
40
41A more complex example can be:
42<enscript highlight=c>
433 + 5 - 12 * 2
44</enscript>
45Is represented in scheme as:
46<enscript highlight=scheme>
47(- (+ 3 5) (* 12 2))
48</enscript>
49
50== Variables
51
52Like 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.
53
54However, 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:
55<enscript highlight=c>
5610 + "5"
57</enscript>
58
59Is in Scheme it would cause an error, and you have to explicitly convert a string to a number:
60<enscript highlight=scheme>
61(+ 10 (string->number "5"))
62</enscript>
63
64
65=== Booleans
66
67TRUE, in Scheme, is noted {{#t}} while FALSE is noted {{#f}}.
68Unlike PHP, all Scheme values are considered to be true ({{#t}}) except {{#f}} itself, including zero ({{0}}), the empty string and other empty structures.
69
70<enscript highlight=scheme>
71(if (- 2 2) "hello" "goodbye")
72;; "hello"
73</enscript>
74
75=== Numbers
76All type of numbers are supported by CHICKEN, including real, complex and rational, natively (without resorting to external libraries). 
77
78Number 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 numbers are decimal by default (so {{21}} and {{#d21}} are the same).
79
80<table>
81 <tr><th>PHP</th><th>scheme</th></tr>
82 <tr><td>21</td><td>#d21</td></tr>
83 <tr><td>025</td><td>#o25</td></tr>
84 <tr><td>0x15</td><td>#x15</td></tr>
85 <tr><td>n/a</td><td>#b10101</td></tr>
86</table>
87
88=== Strings
89
90Scheme support a vast array of operations on string, including changing case, splitting, extracting substring and regular expressions based searching and replacing.
91
92==== Exploding and imploding
93
94<enscript highlight=c>
95implode(';', array('a', 'b', 'c', 'd'))
96</enscript>
97is in CHICKEN Scheme:
98<enscript highlight=scheme>
99(string-intersperse '("a" "b" "c" "d") ";")
100</enscript>
101
102<enscript highlight=c>
103explode(';', 'a;b;c;d')
104</enscript>
105is in CHICKEN Scheme:
106<enscript highlight=scheme>
107(string-split "a;b;c;d" ";")
108</enscript>
109
110Please note that the functions {{string-intersperse}} and {{string-split}} are in the {{(chicken string)}} module, so if you are compiling a program, you will need to add this in the beginning of your code:
111<enscript highlight=scheme>
112(import (chicken string))
113</enscript>
114
115==== Doing several string replacements at once
116
117===== PHP
118<enscript highlight=c>
119str_replace(array('PHP', 'Python'), array('Scheme', 'Clojure'), 'PHP and Python are nice programming languages')
120</enscript>
121
122===== CHICKEN
123
124<enscript highlight=scheme>
125(string-translate* "PHP and Python are nice programming languages" '(("PHP" . "Scheme") ("Python" . "Clojure")))
126</enscript>
127
128For this, too, you'll need to import {{(chicken string)}}.
129
130=== Data structures
131
132The base data structure of PHP is the array.  In Scheme it is the list.
133
134However, 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 hash tables (allow to access elements by any key).
Note: See TracBrowser for help on using the repository browser.