fork download
  1. ; Validating an expression in your DSL
  2. ; ------------------------------
  3. ; The Little Lisper 3rd Edition
  4. ; Chapter 7
  5. ; Exercise 2
  6. ; Common Lisp
  7. ; http://t...content-available-to-author-only...r.com/thelittlelisper
  8. ; http://t...content-available-to-author-only...t.com/2010/06/little-lisper-chapter-7-shadows.html
  9. ; http://t...content-available-to-author-only...t.com/2010/06/little-lisper.html
  10. ; ------------------------------
  11. (setf l1 '())
  12. (setf l2 '(3 + (66 6)))
  13. (setf aexp1 '(1 + (3 * 4)))
  14. (setf aexp2 '((3 ^ 4) + 5))
  15. (setf aexp4 5)
  16. ; ------------------------------
  17.  
  18. ; need to def aexp1 and aexp2
  19.  
  20. (defun operator (aexp_)
  21. (car (cdr aexp_)))
  22.  
  23. (print (operator '(1 + 2)))
  24. ;+
  25.  
  26. (defun isoperator (a)
  27. (cond
  28. ((null a) NIL)
  29. ((eq a '+) t)
  30. ((eq a '*) t)
  31. ((eq a '^) t)
  32. (t NIL)))
  33.  
  34. (print (isoperator '^))
  35. ;T
  36.  
  37. (defun 1st-sub-expr (aexp_)
  38. (car aexp_))
  39.  
  40. (print (1st-sub-expr '(1 + 2)))
  41. ;1
  42.  
  43. (defun 2nd-sub-expr (aexp_)
  44. (car (cdr (cdr aexp_))))
  45.  
  46. (print (2nd-sub-expr '(1 + 2)))
  47. ;2
  48.  
  49. (defun number_ (n)
  50. (cond
  51. ((null n) t)
  52. (t (and
  53. (null (car n))
  54. (number_ (cdr n))))))
  55.  
  56. (print (cons '()(cons '() '())))
  57. ;(NIL NIL)
  58. (print (number_ (cons '()(cons '() '()))))
  59. ;T
  60. ;(number_ (car '(1 + 2)))
  61.  
  62. (defun sub1 (n)
  63. (- n 1))
  64.  
  65. (defun notatom (lat)
  66. (not (atom lat)))
  67.  
  68. (defun number__ (n)
  69. (cond
  70. ((null n) nil)
  71. ((notatom n) nil)
  72. ((= 0 n) t)
  73. (t (number__ (sub1 n)))))
  74.  
  75. (print (number__ 10))
  76. ;T
  77.  
  78. (print (number__ '(66 6)))
  79. ;NIL
  80.  
  81.  
  82. (defun aexp? (aexp_)
  83. (cond
  84. ((null aexp_) NIL)
  85. ((number__ aexp_) t)
  86. ((isoperator (operator aexp_))
  87. (aexp? (1st-sub-expr aexp_))
  88. (aexp? (2nd-sub-expr aexp_))
  89. )
  90. (t NIL)))
  91.  
  92. (print (aexp? aexp1))
  93. ;T
  94.  
  95. (print (aexp? aexp2))
  96. ;T
  97.  
  98. (print (aexp? l1))
  99. ;NIL (false)
  100.  
  101. (print (aexp? l2))
  102. ;NIL (false)
  103.  
Success #stdin #stdout 0.02s 10552KB
stdin
Standard input is empty
stdout
+ 
T 
1 
2 
(NIL NIL) 
T 
T 
NIL 
T 
T 
NIL 
NIL