Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
doc:getting_started:lisp_crash_course [2022/05/03 14:09] khoshnamdoc:getting_started:lisp_crash_course [2022/05/17 11:41] (current) khoshnam
Line 628: Line 628:
  
 **Hello world function** **Hello world function**
-\\ 
 \\ \\
 As any other language we start with hello-world function : As any other language we start with hello-world function :
Line 646: Line 645:
  
 **Defun** **Defun**
-\\ 
 \\ \\
 By using defun we can define new function with different arguments. Any symbol can be used as a function name and it is not mandatory like most of the other languages to only use alphabetic characters for the function name. By using defun we can define new function with different arguments. Any symbol can be used as a function name and it is not mandatory like most of the other languages to only use alphabetic characters for the function name.
Line 662: Line 660:
 </code> </code>
 **Optional arguments** **Optional arguments**
-\\ 
 \\ \\
 Sometime a function might have some parameters which only consider by some of the callers of the function. perhaps because there's a reasonable default value. If in any cases some arguments are optional, in this situation after the names of any required parameters, place the symbol &optional followed by the names of the optional parameters.  Sometime a function might have some parameters which only consider by some of the callers of the function. perhaps because there's a reasonable default value. If in any cases some arguments are optional, in this situation after the names of any required parameters, place the symbol &optional followed by the names of the optional parameters. 
Line 704: Line 701:
  
 **Multiple values** **Multiple values**
-\\ 
 \\ \\
 if in any cases multiple values are needed : if in any cases multiple values are needed :
Line 732: Line 728:
  
 **defvar** **defvar**
-\\ 
 \\ \\
 There are two ways to create global variables in common lisp, defvar and defparameter. Their syntaxs are almost simple by taking variable name, an initial value and an optional documentation string. after that anywhere to refer to the current binding of the global variable.  Notice that global variable's name start and end with *. It is important to mention that defparameter always assign the initial value while defvar does so only if the variable is undefined. We can say if you want to define a variable that you want to keep it even if later you made a change to the source you should use defvar.\\ There are two ways to create global variables in common lisp, defvar and defparameter. Their syntaxs are almost simple by taking variable name, an initial value and an optional documentation string. after that anywhere to refer to the current binding of the global variable.  Notice that global variable's name start and end with *. It is important to mention that defparameter always assign the initial value while defvar does so only if the variable is undefined. We can say if you want to define a variable that you want to keep it even if later you made a change to the source you should use defvar.\\
Line 752: Line 747:
  
 **List** **List**
-\\ 
 \\ \\
 A list is a sequence. The very first element of the list is cons cell. If you want to build a list you should assemble some cons cells together.But before that we will show how you can define a list directly. To define a list we can use : A list is a sequence. The very first element of the list is cons cell. If you want to build a list you should assemble some cons cells together.But before that we will show how you can define a list directly. To define a list we can use :
Line 785: Line 779:
  
 **car/cdr or first/rest** **car/cdr or first/rest**
-\\ 
 \\ \\
 We talked about the cons, however we didn't say that what they  really is?  Cons has a sequential structure that is containing two components that called car and cdr in data structure. Each cons cell is a object that has a pairs of values. The two values in a cons cell are called the car and the cdr. The car function is used to access the first value and the cdr function is used to access the second value. We talked about the cons, however we didn't say that what they  really is?  Cons has a sequential structure that is containing two components that called car and cdr in data structure. Each cons cell is a object that has a pairs of values. The two values in a cons cell are called the car and the cdr. The car function is used to access the first value and the cdr function is used to access the second value.
Line 809: Line 802:
  
 **append** **append**
-\\ 
 \\ \\
 it is possible to add a list or some elements to the list by using append.  it is possible to add a list or some elements to the list by using append. 
Line 823: Line 815:
  
 **subst** **subst**
-\\ 
 \\ \\
 With subst we can substitute any element in a list by other element. With subst we can substitute any element in a list by other element.
Line 878: Line 869:
 **setf & setq** **setf & setq**
 \\ \\
-\\+
 Both of them are used to assign a value to a other data-type, but setq is only using for assign a value to a symbol, however setf can be used for setting value of any data-type and not only symbols. So Setf can be used in place of setq but setq cannot be used in place of setf.  Both of them are used to assign a value to a other data-type, but setq is only using for assign a value to a symbol, however setf can be used for setting value of any data-type and not only symbols. So Setf can be used in place of setq but setq cannot be used in place of setf. 
 Since one data-type will take the value, so setf and setq must have even number of arguments. But the can take any number of pairs.  Since one data-type will take the value, so setf and setq must have even number of arguments. But the can take any number of pairs. 
Line 891: Line 882:
 \\ \\
 **Lambda function** **Lambda function**
-\\ 
 \\ \\
 If you want to define a function without giving it a name it could be possible by using lambda function. Lambda function is also known as anonymous functions.  If you want to define a function without giving it a name it could be possible by using lambda function. Lambda function is also known as anonymous functions. 
Line 912: Line 902:
  
 **if marco** **if marco**
-\\ 
 \\ \\
 The if macro is followed by a test clause that evaluates to t or nil. The if macro is followed by a test clause that evaluates to t or nil.
Line 923: Line 912:
 <code lisp> <code lisp>
 CL-USER>(setf a 10) CL-USER>(setf a 10)
-CL-USER>(if (a 20)+CL-USER>(if (a 20)
   (format t "~% a is less than 20")   (format t "~% a is less than 20")
  (format t "~% a is more than 20"))  (format t "~% a is more than 20"))
Line 947: Line 936:
 \\ \\
 **Progn** **Progn**
-\\ 
 \\ \\
 If you want to do more than one thing in if form, you should use progn. Progn excutes different forms in order and returns the value of the last form. If you want to do more than one thing in if form, you should use progn. Progn excutes different forms in order and returns the value of the last form.
  
 <code lisp> <code lisp>
-CL-USER>(defun *number-was-odd nil)+CL-USER>(defun *number-was-oddnil)
  
 CL-USER>(if (oddp 5) CL-USER>(if (oddp 5)
- (progn (setf (number-was-odd t)+ (progn (setf (*number-was-oddt)
  ‘odd-number)  ‘odd-number)
  ‘even-number)  ‘even-number)
Line 961: Line 949:
  
 **When & Unless** **When & Unless**
-\\ 
 \\ \\
 Instead of using progn, you can use when and unless. With when , all the enclosed expressions are evaluated when the condition is true. With unless , all the enclosed expressions are evaluated when the condition is false. Instead of using progn, you can use when and unless. With when , all the enclosed expressions are evaluated when the condition is true. With unless , all the enclosed expressions are evaluated when the condition is false.
Line 980: Line 967:
  
 **Cond** **Cond**
-\\ 
 \\ \\
 The problem is these commands can’t do anything when the condition evaluates in the opposite way; they just return nil and do nothing. If you want to anything you can use cond macro.  The problem is these commands can’t do anything when the condition evaluates in the opposite way; they just return nil and do nothing. If you want to anything you can use cond macro. 
Line 997: Line 983:
 </code> </code>
  
-<code lisp>+
  
 **defparameter** **defparameter**
 \\ \\
-\\+<code lisp>
 CL-USER>(defparameter *fruit* 'apple) CL-USER>(defparameter *fruit* 'apple)
 CL-USER>(cond ((eq *fruit* 'apple) 'its-an-apple) CL-USER>(cond ((eq *fruit* 'apple) 'its-an-apple)
Line 1008: Line 994:
  
 **Case** **Case**
-\\ 
 \\ \\
 Another lisp command form that can handle different conditions is case that is easier to use.  Another lisp command form that can handle different conditions is case that is easier to use. 
Line 1034: Line 1019:
  
 **And & Or** **And & Or**
-\\ 
 \\ \\
 And and Or operators give true or false. And and Or operators give true or false.
Line 1043: Line 1027:
  
 **Loop in lisp** **Loop in lisp**
-\\ 
 \\ \\
 There are some looping constructions in the lisp. For example, loop, dolist, dotimes and do. There are some looping constructions in the lisp. For example, loop, dolist, dotimes and do.
Line 1050: Line 1033:
  
 **Loop** **Loop**
-\\ 
 \\ \\
 The form-body will evaluate in the loop unless it reaches a limitation or you use return to break it out. The form-body will evaluate in the loop unless it reaches a limitation or you use return to break it out.
Line 1073: Line 1055:
  
 **Dolist** **Dolist**
-\\ 
 \\ \\
 dolist will loop across the item of the list. It is important to mention that the body-form will be evaluated for each item in the list.  dolist will loop across the item of the list. It is important to mention that the body-form will be evaluated for each item in the list. 
Line 1081: Line 1062:
      body-form*)      body-form*)
 </code> </code>
 +The list-form will be the list of elements that are iterated\\
 +The body-form are present in the loop
  
 <code lisp> <code lisp>
Line 1092: Line 1075:
 **Dotimes** **Dotimes**
 \\ \\
-\\ +With dotimes you can do looping for some fixed number of iterations. So the dotimes is quite similar to dolist, but you should consider that it loops for a particular number of times.
-With dotimes you can do looping for some fixed number of iterations.+
  
 <code lisp> <code lisp>
Line 1111: Line 1093:
  
 **Do** **Do**
-\\ 
 \\ \\
 Do is more general than two others.  Do is more general than two others. 
 +
 +<code lisp>
 +CL-USER>(do (var-form*)
 +               (end-form*)
 +                body-form*)
 +</code>
 +Var-form consists of some varibles that we defined and their initial values And how each iteration will affect it\\
 +end-form composed of how the loop finish and value returned. It will evaluated at the start of the iteration.
  
 <code lisp> <code lisp>
Line 1131: Line 1120:
  
 **Defmarco** **Defmarco**
-\\ 
 \\ \\
 One of the greatest thing about lisp is that you can extend the syntax by using defmacro.  One of the greatest thing about lisp is that you can extend the syntax by using defmacro. 
Line 1143: Line 1131:
 \\ \\
 **HASH TABLE** **HASH TABLE**
-\\ 
 \\ \\
 A collection of key-values pairs. It uses the key to access the elements in the collection. Anytime you need to access to some elements only by using a key you can use hash table. \\ A collection of key-values pairs. It uses the key to access the elements in the collection. Anytime you need to access to some elements only by using a key you can use hash table. \\
Line 1269: Line 1256:
 </code> </code>
 **The NIL** **The NIL**
-\\ 
 \\ \\
 First please look at this example. Can you recognize why the output is true? First please look at this example. Can you recognize why the output is true?
Line 1288: Line 1274:
  
 **Let & Let* ** **Let & Let* **
-\\ 
 \\ \\
 In most function definition in lisp you should use let expression and it is a special form in lisp that is used to attach or assign a symbol to a value. In most function definition in lisp you should use let expression and it is a special form in lisp that is used to attach or assign a symbol to a value.
 +<code lisp>
 +CL-USER> (let (variable*)
 +             body-form*)
 +</code>
 +Each variable has an initialization that is assigned by let. In some cases the initialization value will be NIL. For example the following let form will assign 1 5 and NIL to three variables x, y and z.
 +<code lisp>
 +CL-USER> (let ((x 1) (y 5) z)
 +  ...)
 +</code>
 +The value that will return in the body of the let is the value of the last expression.
 +\\
 +Another way to assign a value to a variable is let*. there is a difference between let and let* which is that the variable names can be used in the body of the let however in let* the initial value forms for each variable can refer to variables introduced earlier in the variables list.  For more understanding please consider the following the example :
 +<code lisp>
 +CL-USER>(let* ((a 10)
 +           (b (+ a 10)))
 +              (list a b))
 +</code>
 +
 +<code lisp>
 +CL-USER>(let ((a 10))
 +           (let ((b (+ a 10)))
 +              (list a b)))
 +</code>
 +We can say let do everything in parallel but let* do sequential binding.
 +\\
 +\\
 +**Increament & Decreament**
 +\\
 +There are different ways to increase or decrease a value in common lisp. First you can do this by "+" and "-"
 +<code lisp>
 +CL-USER>(setf x (+ x 1))
 +CL-USER>(setf x (- x 1))
 +</code>
 +However common lisp provided very easiest way to do that by using incf and decf. Please notice that the following examples are exactly the same as two example that is mentioned above.
 +<code lisp>
 +CL-USER>(incf x)
 +CL-USER>(decf x)
 +</code>