From: Jeffrey Mark Siskind
Subject: Re: Permutations - lisp like
Date: 1998/10/12
Newsgroups: comp.lang.lisp
One elegant way of generating permutations (or any other form of combinatoric enumeration) is to write a nondeterministic description of the combinatoric structure. This can be done with Screamer, a nondeterministic extension to Common Lisp.
(defun a-split-of-internal (x y)
(if (null? y)
(list x y)
(either (list x y)
(a-split-of-internal (append x (list (first y))) (rest y)))))
(defun a-split-of (l) (a-split-of-internal '() l))
(defun a-permutation-of (l)
(if (null l)
l
(let ((split (a-split-of (a-permutation-of (rest l)))))
(append (first split) (cons (first l) (second split))))))
(defun permutations-of (l) (all-values (a-permutation-of l)))
You can get Screamer from my home page.
;; Modified from the excellent code found here
;; http://www.shido.info/lisp/scheme_amb_e.html
;; and written by
;; SHIDO, Takafumi
;; [ SHIDO's comment ]
;; Notice that you cannot use the code shown in this chapter if
;; the searching path has loops. See SICP 4.3. for detailed
;; information on this matter.
;;; This function is re-assigned in `amb-choose' and `amb-fail' itself. (define amb-fail #f)
;;; function for nondeterminism
(define (amb-choose . ls)
(if (null? ls)
(amb-fail)
(let ((fail0 amb-fail))
(call/cc
(lambda (cc)
(set! amb-fail
(lambda ()
(set! amb-fail fail0)
(cc (apply amb-choose (cdr ls)))))
(cc (car ls)))))))
;;; nondeterminism macro operator
(define-syntax amb
(syntax-rules ()
((_) (amb-fail))
((_ a) a)
((_ a b ...)
(let ((fail0 amb-fail))
(call/cc
(lambda (cc)
(set! amb-fail
(lambda ()
(set! amb-fail fail0)
(cc (amb b ...))))
(cc a)))))))
;;; returning all possibilities
(define-syntax amb-set-of
(syntax-rules ()
((_ s)
(let ((acc '()))
(amb (let ((v s))
(set! acc (cons v acc))
(amb-fail))
(reverse acc))))))
;; (reverse! acc))))))
;;; if not bool backtrack
(define (amb-assert bool)
(or bool (amb)))
;;; returns arbitrary number larger or equal to n
(define (amb-integer-starting-from n)
(amb n (amb-integer-starting-from (+ 1 n))))
;;; returns arbitrary number between a and b
(define (amb-number-between a b)
(let loop ((i a))
(if (> i b)
(amb)
(amb i (loop (+ 1 i))))))
;; (amb i (loop (1+ i))))))
;;; write following at the end of file
;;; initial value for amb-fail
(call/cc
(lambda (cc)
(set! amb-fail
(lambda ()
(cc 'no-choice)))))
Problem 4.42 in SICP
Five school girls took an exam. As they think thattheir
parents are too much interested in their score, they promise
that they write one correct and one wrong informations to
their parents. Followings are parts of their letters
concerning their result:
Betty: Kitty was the second and I third.
Ethel: I won the top and Joan the second.
Joan: I was the third and poor Ethel the last.
Kitty: I was the second and Mary the fourth.
Mary: I was the fourth. Betty won the top.
Guess the real order of the five school girls.
B. Pym wrote:
Problem 4.42 in SICP
Five school girls took an exam. As they think thattheir
parents are too much interested in their score, they promise
that they write one correct and one wrong informations to
their parents. Followings are parts of their letters
concerning their result:
Betty: Kitty was the second and I third.
Ethel: I won the top and Joan the second.
Joan: I was the third and poor Ethel the last.
Kitty: I was the second and Mary the fourth.
Mary: I was the fourth. Betty won the top.
Guess the real order of the five school girls.
Shorter:
((kitty joan betty mary ethel))
Problem 4.42 in SICP
Five school girls took an exam. As they think thattheir
parents are too much interested in their score, they promise
that they write one correct and one wrong informations to
their parents. Followings are parts of their letters
concerning their result:
Betty: Kitty was the second and I third.
Ethel: I won the top and Joan the second.
Joan: I was the third and poor Ethel the last.
Kitty: I was the second and Mary the fourth.
Mary: I was the fourth. Betty won the top.
Guess the real order of the five school girls.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 403 |
Nodes: | 16 (2 / 14) |
Uptime: | 43:36:42 |
Calls: | 8,407 |
Calls today: | 2 |
Files: | 13,171 |
Messages: | 5,905,025 |