LISP 101
November 4th, 2021 EL Notes Guest on 23rd November 2021 04:00:38 AM
  1. ;;simple list contains atoms
  2. '(1 2 3)
  3. ; (1 2 3)
  4.  
  5. ;;substitute ' for the word quote
  6. (quote (1 2 3))
  7. ; (1 2 3)
  8.  
  9. ;;atoms can be letters too
  10. '(a b c)
  11. ; (a b c)
  12.  
  13. ;;atom returns true if something is an atom
  14. (atom 2)
  15. ; t
  16.  
  17. ;;check if is an atom?? or apply atom to a
  18. (atom 'a)
  19. ; t
  20.  
  21. ;;atom returns false for a list
  22. (atom '(3 4 5))
  23. ; nil
  24.  
  25. (atom '(atom 'a))
  26. ; nil
  27.  
  28. (atom (atom 'a))
  29. ; t
  30.  
  31. (atom (atom '(3 4 5)))
  32. ; t
  33.  
  34. ; nil
  35.  
  36.  
  37. (atom nil)
  38. ; t
  39.  
  40.  
  41. ;;comparisons
  42.  
  43. ;;compare undefined variables
  44. (eq 'a 'b)
  45. ; nil
  46.  
  47. ;;compare avlues
  48. (eq 1 1)
  49. ; t
  50.  
  51. ; comparison of lists will not return true (see eqlist function below)
  52. (eq '(2 3) '(2 3))
  53. ; nil
  54.  
  55.  
  56. ;;car = head of list
  57. ;;cdr = tail of list
  58.  
  59. (car '(4 5 6))
  60. ; 4
  61.  
  62. (cdr '(5 6 7))
  63. ; (6 7)
  64.  
  65. (cdr '( (2 3) (1 2) (5 6 7)))
  66. ; ((1 2) (5 6 7))
  67.  
  68.  
  69. ;;construct list from lists
  70. (cons  '2 '(3 4 5))
  71. ; (2 3 4 5)
  72.  
  73. (cons '(1 2) '(3 4 5))
  74. ; ((1 2)
  75.  
  76. ;; conditional statements
  77. (cond
  78.  ( (eq 'a 'b) '3)
  79.  ((eq '2 '2) '4))
  80. ; 4
  81. ;;returns 4 because a doesn't equal b, but 2 does equal 2
  82. ;;if a equalled b then it would return 3
  83.  
  84. ;;define a function = defun
  85. ;;function that doubles
  86. (defun double (x)
  87.   (* 2 x))
  88.  
  89. (double 3)
  90. ; 6
  91.  
  92.  
  93. ;;returns true if null, false if not null
  94. (null '())
  95. ; t
  96.  
  97. (null '(1))
  98. ; nil
  99.  
  100. ;;list of atoms function
  101. (defun lat (l)
  102.   (cond
  103.    ((null l) t)
  104.    ((atom (car l)) (lat(cdr l)))
  105.    (t nil)
  106.    )
  107.   )
  108.  
  109. (lat '(1 3 3 4))
  110. ; t
  111.      
  112. (lat '(1 (2 3) 4))
  113. ; nil
  114.  
  115. ;;and returns true if everything is true
  116. (and (atom 2) (atom 'a))
  117. ; t
  118.  
  119.  
  120. ;;eqlist function will return true for list of atoms comparisons recursively
  121. (defun eqlist (l1 l2)
  122.   (cond
  123.    ( (and (null l1) (null l2)) t)
  124.    ( (eq (car l1) (car l2)) (eqlist (cdr l1) (cdr l2)))
  125.    (t nil)
  126.    )
  127.   )
  128.  
  129. (eqlist '(1 2 3 4 5) '(1 2 3 4 5))
  130. ; t
  131.  
  132. ;;will return if an atom exists in a list
  133. (defun member2 (x l)
  134.   (cond
  135.    ( (null l) nil)
  136.    ( (eq x (car l)) t)
  137.    ( t (member2 x (cdr l)))
  138.    )
  139.   )
  140.  
  141. (member2 '6 '(1 2 3 4 5))
  142. ; nil
  143.  
  144. (member2 '6 '(2 6 8))
  145. ; t
  146.  
  147. ;;not will invert true/nil statements
  148. (not (eq 2 2))
  149. ;nil
  150.  
  151. (not (eq 1 2))
  152. ;t
  153.  
  154. ;;list of non-atoms, aka list of lists
  155. (defun nonlat (l)
  156.   (cond
  157.    ( (null l) t)
  158.    ( (not (atom (car l))) (nonlat (cdr l)))
  159.    ( t nil)
  160.    )
  161.   )
  162.  
  163. (nonlat '(1 2 3))
  164. ; nil
  165.  
  166. (nonlat '((2 3) 4 5 (5)))
  167. ; nil
  168.  
  169. (nonlat '((2 3) (2 4)))
  170. ; t

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.