LISP 113
November 11th, 2021 EL Notes Guest on 23rd November 2021 06:47:24 AM
  1. ;;pick function will return the nth element in the list l.
  2. (defun pick (n l)
  3.   ( cond
  4.     ( (null l) l)
  5.     ( (eq n 1) (car l))
  6.     ( t ( pick (- n 1) (cdr l)))
  7.   )
  8.  )
  9.  
  10. ;pick demo
  11. (pick 1 '(4 5 6))
  12. ; 4
  13. (pick 4 '(4 5 6))
  14. ;nil
  15.  
  16.  
  17. ;;leftmost function will return the leftmost atom in a given list l.
  18. (defun leftmost (l)
  19.   ( cond
  20.     ( (null l) l)
  21.     ( (atom (car l)) (car l))
  22.     ( t (leftmost (car l)))
  23.   )
  24. )
  25.  
  26. ;leftmost demo
  27. (leftmost '(4 5 6))
  28. ; 4
  29. (leftmost '( (4 5) 6 (3 4)))
  30. ; 4
  31. (leftmost '( ( (2 3) 4) 5 (6 7)))
  32. ; 2
  33. (leftmost '( ( (4) 5) 6 (3 4)))
  34. ; 4
  35.  
  36. ;;car cdr car == cadar
  37. ;;cdr cdr car == cddar
  38.  
  39. (cdr (car (cdr '(9 (3 5) (7 5 (3 4))) ) ) )
  40. ; 5
  41.  
  42. (cdadr '(9 (3 5) (7 5 (3 4))))
  43. ; 5
  44.  
  45. (caadr '( (2 3) (1 2) (5 6 7)))
  46. ; 1
  47.  
  48. (car (car (cdr '((2 3) (1 2) (5 6 7)))))
  49. ; 1
  50.  
  51.  
  52. ;; declare x = 2
  53. (set 'x 2)
  54.  
  55. x
  56. ; 2
  57.  
  58.  
  59. ;; set + quote so you don't need to put the quote before y
  60. (setq y 3)
  61. y
  62. ; 3
  63.  
  64. (+ x y)
  65. ; 5
  66. (* x y)
  67. ; 6
  68.  
  69. ;;you can find a car/cdr of a generated list
  70. (car (list 3 4 'a 'b 6))
  71. ; 3
  72.  
  73. ;;condition with manipulation
  74. ( cond
  75.   ( (> x 5) (+ x 1))
  76.   ( t (- x 1))
  77.   )
  78. ; 1
  79.  
  80. ;;conditions and or not
  81. (if (> x 5) (+ x 1) (- x 1))
  82. ; 1
  83. (if (not (or (> x 5) (< x 0))) (+ x 1) (- x 1))
  84. ; 3
  85. ;same as before with cond
  86.  
  87.  
  88. ;; display maximum item of list
  89. (max 3 5)
  90. ; 5
  91.  
  92. ;; apply operator
  93. (apply '+ '(3 4 5))
  94. ; 12
  95.  
  96.  
  97. ;;you can substitute ' with quote
  98. (setq op '*)
  99. ; *
  100.  
  101. ;;substitute our variable op for *
  102. (apply op '(3 4 5))
  103. ; 60
  104.  
  105. ;;same but with max
  106. (setq op2 'max)
  107. ; max
  108. (apply op2 '(3 4 5))
  109. ; 5
  110. (apply op2 '(4 7 3))
  111. ; 7
  112.  
  113. ;; returns true if is a number
  114. (numberp 4)
  115. ; t
  116.  
  117. (numberp 'a)
  118.  
  119.  
  120. (numberp '(3 4))
  121. ; nil
  122.  
  123. (numberp 100)
  124. ; t
  125.  
  126.  
  127. ;;returns items in list that aren't numbers
  128. (defun no-num (l)
  129.   ( cond
  130.     ( (null l) l)
  131.     ( (numberp (car l)) (no-num (cdr l)))
  132.     ( t (cons (car l) (no-num (cdr l))))
  133.     )
  134.   )
  135.  
  136.  
  137. (no-num '(1 'a 'b 3 'flower))
  138. ; ('a 'b 'flower)
  139.  
  140. (no-num '(3 5 6))
  141. ; nil
  142.  
  143. x
  144. ; 2
  145. y
  146. ; 3
  147. (no-num '(a b c x y))
  148. ; (a b c x y)
  149. (no-num '(5 'a 'b 'flower 100 15 'car))
  150. ; ('a 'b 'flower 'car)

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.