Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

LISP:Control Structures In Lisp


Published on

LISP:Control Structures In Lisp

Published in: Technology
  • Login to see the comments

  • Be the first to like this

LISP:Control Structures In Lisp

  1. 1. Control structures in Lisp<br />
  2. 2. Common Lisp provides with a variety of special structures for organizing the programs.Some have to do with the flow of control. These are called as control structures.<br />Locally defined functions(flet, labels) and macros( macrolet) are quite flexible.<br />CL provides:<br /><ul><li> simple one-way conditionals (when and unless.)
  3. 3. the simple two-way conditionals (if)
  4. 4. more general multi-way conditionals( cond, case)</li></ul>Constructs for performing non local exits with various scoping disciplines are provided: block, return, return-from, catch and throw.<br />
  5. 5. overview<br />Constants and variables<br />References<br />Assignment<br />Generalized variables<br />Function invocation<br />Simple sequencing<br />Conditionals<br />Blocks and Exits<br />Iterations <br />Mapping<br />
  6. 6. Constants and variables<br />CL provides two kinds of variables: ordinary variables and function names. One of them is used to name defined function, macros, and special forms , and the other to name data objects.<br /> The name of the ordinary variable may be obtained by simply writing the name of the variable as a form to be executed.<br />The following functions and variables allow reference to the values of constants and variables:<br />quote object simply returns the object<br />function fn if fn is a symbol, the functional definition associated with that symbol is returned. If fn is a lambda-expression, then a “lexical closure” is returned.<br />
  7. 7. References<br />Symbol-value symbol returns the dynamic current value of the symbol, error occurs if the symbol has no values.<br />Symbol-function-symbol returns the current global function definition named by the symbol, error is signaled if the symbol has no function definition.<br />Symbol-function is applicable only for global functions its not applicable for lexical function name produced by flet or labels.<br />
  8. 8. Symbol-function can be called on any function on which fbound function is true.<br />fbound in turn returns true for symbols which define a macro or a special form.<br />When symbol-function is used with typef, its new value must be type function.<br />fdefinition function-name returns the current global function definition named by the argument function name.<br />
  9. 9. boundp symbol returns true if the symbol has a value, else returns false.<br />fboundp symbol returns true if the function has global function definition<br />Special-form-p symbol returns a non-nil value if the symbol globally names a special form, else returns nil<br />
  10. 10. Assignment<br />The following allow the value associated with the current binding of the variable to be changed.<br />(setq var1 form1 var2 form2 ….) here first the form1 is calculated and the value is stored in val1, then form2 is calculated and stored in form2 and so on..<br />Setq returns the last value assigned, there must be even number of arguments forms.<br />Ex: (setq x (* 3 2) y (cons x nil))retains the first set value of x as 6 and returns 6.<br />Psetq {form}* is similar to setq except that assignments happen in parallel.<br />Ex: (setq a 4)<br /> (setq b 6)<br /> (psetq a b b a) a=6 and b=4, thus the values of a and b are exchanged by using parallel assignment<br />
  11. 11. Set symbol value1  allows the alteration of the dynamic variable , set allows the symbol to take value1 as its value.<br />Ex: (set (if (eq a b) ‘c ‘d) ‘foo) sets either c to foo or d to foo depending on the outcome of the result, set returns the value as a result.<br />makeunbound symbol causes the dynamic(special) variable named by symbol to become unbound.<br />fmakeunbound symbol makes the analogous thing for the global function definition named by symbol.<br />
  12. 12. Generalized variables<br />The concept of the variables named by symbols can be generalized to any storage location that can remember one piece of data.<br />Ex of such storage locations are the car and cdr of cons.<br />Basic operations on variables are: access and update operations.<br />The following table give the various access and update functions:<br />
  13. 13. Setf is a macro that that examines an access form and produces a call to corresponding update function.<br />Setf {place new-value}* takes a form place that when evaluated accesses a data object in some location and “inverts” it to produce a corresponding form to update the location.<br />If more than one place-newvalue pair is specified, the pairs are processed sequentially; that is<br />(setf place1 newvalue1,<br /> place2 newvalue2,<br /> ……<br /> placen newvaluen)<br />
  14. 14. Rotatef{place}* each place form may be any form acceptable as a generalized variable to setf.<br />In the form (rotatef place1,place2,...placen) the values of place1 to placen are being accessed and saved.<br />It is as if all the places from an end-round shift register that is rotated one place to the left, with the value of place1 being shifted around to the end to placen. <br />(setf place1 value1 place2 value2…) the subforms of place1 and value1 are evaluated, the location specified by place1 is modified to contain the value returned by value1, and then the rest of setf form is processed in a like manner.<br />
  15. 15. Function invocation<br />Any list that has no other interpretation to as a macro call or special form is taken to be a function call.<br />This applies function to a list of arguments.<br />The arguments of the function consists of the last arguments to apply appended to the end of the list of all the other arguments to apply but the function itself.<br />Ex: (setf a ‘*) (apply a ‘( 3 2))6<br />If the function takes keyword arguments , the keywords as well as the corresponding values must appear in the argument list.<br />Ex: (apply # ‘(lamda (&key a b) (list a b)) ‘(:b 3))(nil 3)<br />apply function arg &rest more-args<br />
  16. 16. (funcall fn a1,a2,a3……an) applies the function fn to arguments a1,a2,….an<br />Ex: (cons 2 3)( 2 . 3 )<br /> (setq cons (symbol-function ‘+)#<Function +><br /> (funcall cons 2 3) 5<br />funcall fn &rest arguments<br />
  17. 17. Simple sequencing<br />These constructs simply evaluate all the argument forms in order.<br /> progn {form}*  progn construct takes a number of forms and evaluates them sequentially in order from left to right.<br />The values of all forms but the last are discarded, whatever the last form evaluates is returned as the result.<br /> prog1 first {form}*  is similar to progn but it returns the value of its first form.<br />prog2 first second {form}* is similar to prog1, but it returns the value of the second form.<br /> prog2 ( a b c d…….) is similar to (prog a( prog1 b c…..z))<br />
  18. 18. conditionals<br />The traditional conditional construct in lisp is cond.<br />CL also produces the dispatching constructs case and typecase.<br />If test then [else] first the form is evaluated. If the result is not nil, then the form is selected.<br />(If test then else) is similar to (cond (test then) (t else))<br />(when test form1 form2…)first evaluates test. <br />If the result is nil, then no form is evaluated, and nil is returned.<br />Otherwise the forms constitute an implicit progn and are evaluated sequentially from left to right, and the value of the last one is returned .<br />
  19. 19. (unless test form1 form2….) first evaluates test. If the result is not nil, then the forms are not evaluated, and nill is returned.<br />Otherwise the form constitutes an implicit progn and are evaluated sequentially from left to right, and the value of the last one is returned.<br />(cond (p…)<br /> (q…)<br /> (r…)<br /> ………<br /> (t…))<br />Is similar to if p then…<br /> else if q then….<br /> else if r then…..<br /> …..<br /> else……<br />
  20. 20. typecase keyform {(type {form}* )}* typecase is a conditional that chooses one of it clauses by examining the type of an object. Its form is as follows:<br />(typecase keyform<br /> (type-1 consequent-1-1 consequent-1-2…..)<br /> (type-2 consequent-2-1 …..)<br /> (type-3 consequent-3-1 ….)<br /> ….. )<br />
  21. 21. Blocks and exits<br />The block and return-form constructs provide a structured lexical non-local exit facility.<br />block name {form}* the block construct executes each form from left to right, returning whatever is returned by the last form.<br />
  22. 22. return-from name [result] return-from is used to return from a block or from such constraints as do and prog that implicitly establish a block.<br />The name is not evaluated and must be a symbol.<br />The evaluation of result produces multiple values, those multiple values are returned by the construct exited.<br />return [result] (return form) is identical in meaning to (return-from nil form)<br />It returns from a block named nil.<br />
  23. 23. Iteration<br />Loop construct provides a trivial iteration facility. It controls no variables, and simply executed its body repeatedly.<br />loop {form}* each form is evaluated from left to right, when the last form has been evaluated, then the first form is evaluated again, and so on in a never ending cycle.<br />Its execution must be terminated explicitly, using return or throw.<br />
  24. 24. Do and do*<br />Macro syntax:<br />do ({var [init [step]])}*)<br /> (end-test {result}*)<br /> {declaration}* {tag|statement}*<br />do* ({var [init [step]])}*)<br /> (end-test {result}*)<br /> {declaration}* {tag|statement}*<br /> The do special form provides a generalized iteration facility, with an arbitrary number of “index variables”.<br />
  25. 25. Do form looks like this<br />(do ((var1 init1 step1)<br /> (var1 init1 step1)<br /> …….<br />(var1 init1 step1)<br /> (end-test . result)<br /> {declaration}*<br /> .tagbody)<br /> The do* looks exactly the same except that the name do is replaced by do*.<br />
  26. 26. Mapping<br />Mapping is type of iteration in which a function is successively applied to pieces of one or more sequences.<br />The result of the iteration is a sequence containing the respective results of the function applications.<br />The function map may be used to map over any kind of sequence.<br />
  27. 27. The following functions operate only on lists:<br />mapcar function list &rest more-lists<br />maplist function list &rest more-lists<br />mapcfunction list &rest more-lists<br />mapl function list &rest more-lists<br />mapcan function list &rest more-lists<br />mapcon function list &rest more-lists<br />
  28. 28. mapcar function list &rest more-lists<br /><ul><li>mapcar operates on successive elements of lists, first the function is applied to the function of each list, then to the cadr of each list, and so on.
  29. 29. The value returned by the mapcar is a list of the results of the successive calls to the function.</li></ul>Ex: (mapcar #’abs ‘(3 -4 2 -5 -6))(3 4 2 5 6) (mapcar #’ cons ‘(a b c) ‘(1 2 3))((a.1) (b.2) (c.3))<br />
  30. 30. maplist function list &rest more-lists<br /><ul><li>maplist is similar to mapcar except that the function is applied to the lists and successive elements of the lists.</li></ul>Ex: (maplist #’ (lambda (x) (cons ‘foo x))<br /> ‘(a b c d))<br /> ((foo a b c d) (foo b c d) (foo c d) (foo d))<br />
  31. 31. Visit more self help tutorials<br />Pick a tutorial of your choice and browse through it at your own pace.<br />The tutorials section is free, self-guiding and will not involve any additional support.<br />Visit us at<br />