LISP 13
Untitled By dsimms360 on 11th November 2021 10:12:19 PM
  1. ;;11/11/21
  2.  
  3. ;;this function will return the nth element in the list l.
  4. (defun pick (n l)
  5.   ( cond
  6.     ( (null l) l)
  7.     ( (eq n 1) (car l))
  8.     ( t ( pick (- n 1) (cdr l)))
  9.   )
  10. )
  11.  
  12. (pick 1 '(4 5 6))
  13. (pick 4 '(4 5 6))
  14.  
  15. ;;this function will return the leftmost atom in a given list l.
  16. (defun leftmost (l)
  17.   ( cond
  18.     ( (null l) l)
  19.     ( (atom (car l)) (car l))
  20.     ( t (leftmost (car l)))
  21.   )
  22. )
  23.  
  24. (leftmost '(4 5 6))
  25. (leftmost '( (4 5) 6 (3 4)))
  26. (leftmost '( ( (2 3) 4) 5 (6 7)))
  27. (leftmost '( ( (4) 5) 6 (3 4)))
  28.  
  29.  
  30. (cdr (cdr (cdr '(9 (3 5) (7 5 (3 4))) ) ) )
  31.  
  32. (set 'x 2)
  33. x
  34.  
  35. (setq y 3)
  36. y
  37. x
  38. (+ x y)
  39. (* x y)
  40.  
  41. (setq z (* x y))
  42. z
  43.  
  44.  
  45. (car (list 3 4 'a 'b 6))
  46.  
  47.  
  48. ( cond
  49.   ( (> x 5) (+ x 1))
  50.   ( t (- x 1))
  51. )
  52.  
  53. (if (not (or (> x 5) (< x 0))) (+ x 1) (- x 1))
  54.  
  55. (max 3 5)
  56.  
  57. (apply '+ '(3 4 5))
  58.  
  59. ;;does not work. Invalid syntax. 3 4 5 need to be in a list --> '(3 4 5)
  60. (apply '+ 3 4 5)
  61.  
  62. (setq op '*)
  63.  
  64. (apply op '(3 4 5))
  65.  
  66. (setq op2 'max)
  67.  
  68. (apply op2 '(3 6 9))
  69.  
  70.  
  71. (numberp 4)
  72.  
  73. (numberp 'a)
  74.  
  75. (numberp '(3 4))
  76.  
  77. (numberp 100)
  78.  
  79. ;;this function will return a list of the non-number members of the given list.
  80. (defun no-num (l)
  81.   ( cond
  82.      ( (null l) l)
  83.      ( (numberp (car l)) (no-num (cdr l)))
  84.      ( t (cons (car l) (no-num(cdr l))))
  85.   )
  86. )
  87.  
  88. (no-num '(1 'a 'b 3 'flower))
  89. (no-num '(5 'a 'b 'flower 100 15 '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.