133 | | What is underlying representation of this? Bunch of tags and pointers.) |
134 | | |
| 133 | What is ''underlying machine representation'' of this? Lets call it a ''bunch of tags and pointers'' for now.) |
| 134 | |
| 135 | Now lets consider some ''functions'' |
| 136 | {{{ |
| 137 | y = x^2 |
| 138 | }}} |
| 139 | or |
| 140 | {{{ |
| 141 | f(x) = x^2 |
| 142 | }}} |
| 143 | Some famous function have their own names such as ''sin'' or ''cos'' or ''exp'' or ''sqrt'' but the wast majority are nameless, anonymous, too ordinary. |
| 144 | |
| 145 | The way to represent a function without name is by writing down its body: |
| 146 | {{{ |
| 147 | x * x |
| 148 | }}} |
| 149 | or in the ''prefix notation'' |
| 150 | {{{ |
| 151 | (* x x) |
| 152 | }}} |
| 153 | So, an ''expression'' (of a function) is mere a ''list'' of ''symbols''. Symbols are just ''names'' of (''tags'' or ''pointers'' to) something, their meaning are their ''values'' - what is behind a name. |
| 154 | |
| 155 | To evaluate an ''expression'' is to get the ''value'' of that expression. For a ''symbol'' (a ''name'') it will be its ''value'' (a ''number'' or a ''list'' - another ''expression''). There is no limit. It is called recursion. |
| 156 | |
| 157 | There is some special notation in Math for anonymous functions - the ''lambda notation''. Because it is boring to write Greek symbols on qwerty-keyboard, people are using the word ''lambda'' instead with the same meaning: |
| 158 | {{{ |
| 159 | (lambda (x) |
| 160 | (* x x)) |
| 161 | }}} |
| 162 | which means "there is a procedure without a name, with one parameter named 'x' which multiplies it by itself". |
| 163 | |
| 164 | This ''lambda expression'' then (which is a mere list of lists) is the ''special form'' to define a procedure without a name. |
| 165 | |
| 166 | Now, how to evaluate a procedure without a name? The only way is just to write it down here and then invoke it with an actual value (3): |
| 167 | {{{ |
| 168 | ((lambda (x) |
| 169 | (* x x)) |
| 170 | 3) |
| 171 | }}} |
| 172 | This is the small miracle of the ''prefix notation'' - this is why we write {{{(* x x)}}} instead of {{{(x * x)}}}. |
| 173 | |
| 174 | The reason is that the ''body'' of a procedure (which is a list of expressions) always comes as the ''first element'' ('''car''') of the invocation expression (which is a list itself). |
| 175 | |
| 176 | The rest ('''cdr''') of the invocation expression is the ''list'' of ''values'' of parameters of a procedure. This list could be ''infinite'' (it is limited only by hardware) or even ''undefined'' (to be produced at the time of invocation ''on demand''). |
| 177 | |
| 178 | Now lets say it: '''To square something is to multiply it by itself'''. |
| 179 | {{{ |
| 180 | ; the definition of square: |
| 181 | ; |
| 182 | ; TO SQUARE SOMETHING |
| 183 | ; \ | |
| 184 | (define (square x) (* x x)) |
| 185 | ; / | \ |
| 186 | ; MULTIPLY IT BY ITSELF |
| 187 | }}} |
| 188 | Which is just an abbreviation for: define a procedure named ''square'' |
| 189 | to be a multiplication of the parameter named ''x'' by itself: |
| 190 | {{{ |
| 191 | ; WITH AN ARGUMENT |
| 192 | ; / NAMED X |
| 193 | (define square |
| 194 | (lambda (x) (* x x))) |
| 195 | ; | | |
| 196 | ; MAKE A PROCEDURE | |
| 197 | ; | |
| 198 | ; THAT RETURNS THE |
| 199 | ; RESULT OF |
| 200 | ; MULTIPLYING X BY X |
| 201 | }}} |
| 202 | Everything is a list of expressions (of lists). This is the symbolic |
| 203 | representation (using the prefix notation) of.. of everything. Code is |
| 204 | a list of expressions, expressions are lists themselves. Code is data. |
| 205 | |
| 206 | This is called the unity of the language. |
| 207 | |