Changes between Version 2 and Version 3 of LispNotation


Ignore:
Timestamp:
Apr 4, 2012, 12:36:44 PM (10 years ago)
Author:
schiptsov
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • LispNotation

    v2 v3  
    131131What is ''the type'' of this expression? Well, it is a ''table'', '''and''' it is an ''associative list'', '''and''' it is a ''list of pairs'', '''and''' it is ''list of lists''.
    132132
    133 What is underlying representation of this? Bunch of tags and pointers.)
    134 
     133What is ''underlying machine representation'' of this? Lets call it a ''bunch of tags and pointers'' for now.)
     134
     135Now lets consider some ''functions''
     136{{{
     137y = x^2
     138}}}
     139or
     140{{{
     141f(x) = x^2
     142}}}
     143Some 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
     145The way to represent a function without name is by writing down its body:
     146{{{
     147x * x
     148}}}
     149or in the ''prefix notation''
     150{{{
     151(* x x)
     152}}}
     153So, 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
     155To 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
     157There 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}}}
     162which means "there is a procedure without a name, with one parameter named 'x' which multiplies it by itself".
     163
     164This ''lambda expression'' then (which is a mere list of lists) is the ''special form'' to define a procedure without a name. 
     165
     166Now, 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}}}
     172This is the small miracle of the ''prefix notation'' - this is why we write {{{(* x x)}}} instead of {{{(x * x)}}}.
     173
     174The 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
     176The 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
     178Now 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}}}
     188Which is just an abbreviation for: define a procedure named ''square''
     189to 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}}}
     202Everything is a list of expressions (of lists). This is the symbolic
     203representation (using the prefix notation) of.. of everything. Code is
     204a list of expressions, expressions are lists themselves. Code is data.
     205
     206This is called the unity of the language.
     207