Changes between Version 5 and Version 6 of Tutorial0

Nov 17, 2012, 9:42:55 AM (9 years ago)



  • 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.
     112'''anonymous helper procedure'''
     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.
     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.
     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.
     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))))))
     126This is how anonymous helper procedure makes small miracles.
     128(keep (lambda (x) (zero? (remainder x 2))) '(1 2 3 4 5 6))
     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.
     132It is almost like Math - we could write a function as it is - just {{{x^2 + 2x + 3}}}