Changes between Version 5 and Version 6 of Tutorial0


Ignore:
Timestamp:
Nov 17, 2012, 9:42:55 AM (9 years ago)
Author:
schiptsov
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Tutorial0

    v5 v6  
    110110So, the global environment is just a list of pre-defined bindings and other frames - a list of lists. Each frame is a list of bindings. Everything in Lisp logically is a list of lists made out of pairs. This is one of the sources of its magic - we could see the structure of everything.
    111111
     112'''anonymous helper procedure'''
     113
     114Sometimes we need a small procedure to perform an operation for each element of a list. We could, of course, define and bind it to some symbol somewhere else, and then just use its name. But in order to see what it does, we must find its source.
     115
     116If a procedure is small enough, we prefer to create it right there, in-place, to see the whole thing at once. This is why we need lambda expressions - to make our code self-evident and easy to comprehend.
     117
     118This is a procedure that applies given ''predicate function'' to each element of given list. It returns only elements for which application of predicate returns true. This is essentially a filter.
     119{{{
     120(define keep (lambda (f l)
     121                 (cond ((null? l) '())
     122                       ((f (car l)) (cons (car l)
     123                                          (keep f (cdr l))))
     124                       (else (keep f (cdr l))))))
     125}}}
     126This is how anonymous helper procedure makes small miracles.
     127{{{
     128(keep (lambda (x) (zero? (remainder x 2))) '(1 2 3 4 5 6))
     129}}}
     130We are defining a predicate function we need right there, in-place. Because the body of a procedure is self-evident, we have no necessity to name it.
     131
     132It is almost like Math - we could write a function as it is - just {{{x^2 + 2x + 3}}}
     133
    112134'''let'''
    113135