Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. (defunc how-many (x l)
  2. :input-contract (listp l)
  3. :output-contract (natp (how-many x l))
  4. (cond
  5. ((endp l) 0)
  6. (t
  7. (if (equal x (first l))
  8. (+ 1 (how-many x (rest l)))
  9. (how-many x (rest l))))))
  10.  
  11. (check= (how-many 1 '(1 2 3 4)) 1)
  12. (check= (how-many 5 '(1 2 3 4)) 0)
  13. (check= (how-many 'a '(a b a b b b)) 2)
  14. (check= (how-many 'a '(b a b b a)) 2)
  15.  
  16. (defunc find-index (l x n hm)
  17. :input-contract (and (listp l) (natp n) (natp hm))
  18. :output-contract (or (natp (find-index l x n hm)) (equal nil (find-index l x n hm)))
  19. (cond
  20. ((endp l) nil)
  21. ((and (equal (first l) x) (equal hm 0)) n)
  22. ((equal (first l) x) (find-index (rest l) x (+ n 1) (- hm 1)))
  23. (t (find-index (rest l) x (+ n 1) hm))))
  24.  
  25.  
  26. (check= (find-index '(a b c d) 'a 0 0) 0)
  27. (check= (find-index '(a b c d) 'd 0 0) 3)
  28. (check= (find-index '(a b c d a) 'a 0 1) 4)
  29. (check= (find-index '(a b a c c d a) 'a 0 2) 6)
  30. (check= (find-index '(a b a c c d a) 'a 0 3) nil)
  31. (check= (find-index '(a b a c c d a) 'e 0 0) nil)
  32.  
  33. (defunc find-arrangement/acc (x y acc)
  34. :input-contract (and (listp x) (listp y) (listp acc))
  35. :output-contract (or (listp (find-arrangement/acc x y acc)) (equal nil (find-arrangement/acc x y acc)))
  36. (cond
  37. ((endp y) ())
  38. ((equal nil (find-index x (first y) 1 (how-many (first y) acc))) nil)
  39. (t (cons (find-index x (first y) 1 (how-many (first y) acc)) (find-arrangement/acc x (rest y) (cons (first y) acc))))))
  40.  
  41. (defunc find-arrangement (x y)
  42. :input-contract (and (listp x) (listp y))
  43. :output-contract (or (listp (find-arrangement x y)) (equal nil (find-arrangement x y)))
  44. (if (equal (len (find-arrangement/acc x y ())) (len y))
  45. (find-arrangement/acc x y ())
  46. nil))
  47.  
  48.  
  49.  
  50. (check= (find-arrangement '(a b c e f g) '(a b c e f g g)) nil)
  51. (check= (find-arrangement '(a b c) '(a c b)) '(1 3 2))
  52. (check= (find-arrangement '(a a) '(a a)) '(1 2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement