Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defunc how-many (x l)
- :input-contract (listp l)
- :output-contract (natp (how-many x l))
- (cond
- ((endp l) 0)
- (t
- (if (equal x (first l))
- (+ 1 (how-many x (rest l)))
- (how-many x (rest l))))))
- (check= (how-many 1 '(1 2 3 4)) 1)
- (check= (how-many 5 '(1 2 3 4)) 0)
- (check= (how-many 'a '(a b a b b b)) 2)
- (check= (how-many 'a '(b a b b a)) 2)
- (defunc find-index (l x n hm)
- :input-contract (and (listp l) (natp n) (natp hm))
- :output-contract (or (natp (find-index l x n hm)) (equal nil (find-index l x n hm)))
- (cond
- ((endp l) nil)
- ((and (equal (first l) x) (equal hm 0)) n)
- ((equal (first l) x) (find-index (rest l) x (+ n 1) (- hm 1)))
- (t (find-index (rest l) x (+ n 1) hm))))
- (check= (find-index '(a b c d) 'a 0 0) 0)
- (check= (find-index '(a b c d) 'd 0 0) 3)
- (check= (find-index '(a b c d a) 'a 0 1) 4)
- (check= (find-index '(a b a c c d a) 'a 0 2) 6)
- (check= (find-index '(a b a c c d a) 'a 0 3) nil)
- (check= (find-index '(a b a c c d a) 'e 0 0) nil)
- (defunc find-arrangement/acc (x y acc)
- :input-contract (and (listp x) (listp y) (listp acc))
- :output-contract (or (listp (find-arrangement/acc x y acc)) (equal nil (find-arrangement/acc x y acc)))
- (cond
- ((endp y) ())
- ((equal nil (find-index x (first y) 1 (how-many (first y) acc))) nil)
- (t (cons (find-index x (first y) 1 (how-many (first y) acc)) (find-arrangement/acc x (rest y) (cons (first y) acc))))))
- (defunc find-arrangement (x y)
- :input-contract (and (listp x) (listp y))
- :output-contract (or (listp (find-arrangement x y)) (equal nil (find-arrangement x y)))
- (if (equal (len (find-arrangement/acc x y ())) (len y))
- (find-arrangement/acc x y ())
- nil))
- (check= (find-arrangement '(a b c e f g) '(a b c e f g g)) nil)
- (check= (find-arrangement '(a b c) '(a c b)) '(1 3 2))
- (check= (find-arrangement '(a a) '(a a)) '(1 2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement