Hi
Thanks for all the answers I get for the macro question.
I couldn't get any help from scheme group.
If lisp can do amb as well then my problem is solved.
This is the particular problem (not homework) I'm trying to solve:
This is like solving variable length permutation which is hard to do
without in place code expansion. You can use a single number to map
permutation but it's ugly.
;; let n = number of initial conditions n>=1
;; l = number of non zero terms (matrix size) l>=1
;; v = number of verification terms v>=1
;; total terms = tt = n + l + v
;; l = g (glob_terms) + c (constant term 0|1 )
;; g >= n (all initial conditions must be used)
(define (solve-gen tt)
(define hd 2)
(let ((n (number-between 1 tt))
(v (number-between 1 tt))
(l (number-between 1 tt))
)
(assert (= (+ n l v) tt) )
(let ((g (number-between 1 l))
(c (number-between 0 1))
)
(assert (= (+ g c) l) )
(list n v l g c)
;; all dimensions are locked in
;; I need to do in-place code expansion
;; (make-list g (make-list n (number-between 0 1)))
)
)
)
(bag-of (list (number-between 0 1) (number-between 0 1) ))
(bag-of (list (number-between 0 1) (number-between 0 1) (number-
between 0 1) ))
> ((0 0) (0 1) (1 0) (1 1))
> ((0 0 0) (0 0 1) (0 1 0) (0 1 1) (1 0 0) (1 0 1) (1 1 0) (1 1 1))
;; as you can see the code would only work if can expand code in place
dynamically n times.
·········@gmail.com wrote:
> Thanks for all the answers I get for the macro question.
> I couldn't get any help from scheme group.
> If lisp can do amb as well then my problem is solved.
> This is the particular problem (not homework) I'm trying to solve:
> This is like solving variable length permutation which is hard to do
> without in place code expansion. You can use a single number to map
> permutation but it's ugly.
>
> ;; let n = number of initial conditions n>=1
> ;; l = number of non zero terms (matrix size) l>=1
> ;; v = number of verification terms v>=1
> ;; total terms = tt = n + l + v
> ;; l = g (glob_terms) + c (constant term 0|1 )
> ;; g >= n (all initial conditions must be used)
> (define (solve-gen tt)
> (define hd 2)
> (let ((n (number-between 1 tt))
> (v (number-between 1 tt))
> (l (number-between 1 tt))
> )
> (assert (= (+ n l v) tt) )
> (let ((g (number-between 1 l))
> (c (number-between 0 1))
> )
> (assert (= (+ g c) l) )
> (list n v l g c)
> ;; all dimensions are locked in
> ;; I need to do in-place code expansion
> ;; (make-list g (make-list n (number-between 0 1)))
> )
> )
> )
>
> (bag-of (list (number-between 0 1) (number-between 0 1) ))
> (bag-of (list (number-between 0 1) (number-between 0 1) (number-
> between 0 1) ))
>> ((0 0) (0 1) (1 0) (1 1))
>> ((0 0 0) (0 0 1) (0 1 0) (0 1 1) (1 0 0) (1 0 1) (1 1 0) (1 1 1))
>
> ;; as you can see the code would only work if can expand code in place
> dynamically n times.
>
Sorry; I'm not familiar with your terminology... Your "amb" is similar
to the following link, right?
http://www.rubyquiz.com/quiz70.html
If so, then its readily implemented in Lisp. I've done similar things
to generate backtracking parsers from an EBNF grammar... You might
enjoy a couple books:
Paradigms of Artificial Intelligence Programming by Peter Norvig
- chapter 11, Logic Programming.
http://norvig.com/paip.html
On Lisp by Paul Graham
- chapter 22, Nondeterminism.
http://paulgraham.com/onlisp.html
- Daniel
D Herring wrote:
> ·········@gmail.com wrote:
>> Thanks for all the answers I get for the macro question.
>> I couldn't get any help from scheme group.
>> If lisp can do amb as well then my problem is solved.
...
> Sorry; I'm not familiar with your terminology... Your "amb" is similar
> to the following link, right?
> http://www.rubyquiz.com/quiz70.html
Oh how strongly Ruby programmers lust after Lisp:
http://www.rubyquiz.com/quiz95.html
·········@gmail.com wrote:
> (bag-of (list (number-between 0 1) (number-between 0 1) ))
> (bag-of (list (number-between 0 1) (number-between 0 1) (number-
> between 0 1) ))
>> ((0 0) (0 1) (1 0) (1 1))
>> ((0 0 0) (0 0 1) (0 1 0) (0 1 1) (1 0 0) (1 0 1) (1 1 0) (1 1 1))
>
> ;; as you can see the code would only work if can expand code in place
> dynamically n times.
If you just want to calculate all possible combinations, you don't need
macros:
(defun combinations-caller (max count fun &optional (item '()))
(if (> count 0)
(loop for i from 0 to max do
(combinations-caller max (1- count) fun (cons i item)))
(funcall fun item)))
(defun combinations (max count)
(let ((list '()))
(combinations-caller max
count
(lambda (item) (push item list)))
list))
CL-USER > (combinations 1 2)
((1 1) (0 1) (1 0) (0 0))
CL-USER > (combinations 1 3)
((1 1 1) (0 1 1) (1 0 1) (0 0 1) (1 1 0) (0 1 0) (1 0 0) (0 0 0))
CL-USER > (combinations 2 2)
((2 2) (1 2) (0 2) (2 1) (1 1) (0 1) (2 0) (1 0) (0 0))
If you are searching for a general list comprehension algorithm, macros
could be useful, like described in a talk at the ILC2007:
http://www.international-lisp-conference.org/2007/speakers#latendresse_mario
The talk was very interesting and it was impressive how small the macro was
to implement list comprehensions like used e.g. in Haskell.
(combinations 2 2) would look like this:
((list x y) (x <- '(0 1 2)) (y <- '(0 1 2)))
I have the paper in printed form (they gave all ILC participants a nice
book with all articles), maybe it is available on the web, too.
--
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
On Apr 17, 12:56 am, Frank Buss <····@frank-buss.de> wrote:
> ·········@gmail.com wrote:
> > (bag-of (list (number-between 0 1) (number-between 0 1) ))
> > (bag-of (list (number-between 0 1) (number-between 0 1) (number-
> > between 0 1) ))
> >> ((0 0) (0 1) (1 0) (1 1))
> >> ((0 0 0) (0 0 1) (0 1 0) (0 1 1) (1 0 0) (1 0 1) (1 1 0) (1 1 1))
>
> > ;; as you can see the code would only work if can expand code in place
> > dynamically n times.
>
> If you just want to calculate all possible combinations, you don't need
> macros:
The problem is not really about generating combination or
permutations.
I can always map permutation with a single integer. (ugly)
(I don't want to use coroutines either, the code become difficult to
read. ugly)
It's about dynamic code expansion.
If I can use library I could've used Maple.
I've just got help from scheme group and the code is working.
So, no show this time, sorry.
>
> (defun combinations-caller (max count fun &optional (item '()))
> (if (> count 0)
> (loop for i from 0 to max do
> (combinations-caller max (1- count) fun (cons i item)))
> (funcall fun item)))
>
> (defun combinations (max count)
> (let ((list '()))
> (combinations-caller max
> count
> (lambda (item) (push item list)))
> list))
>
> CL-USER > (combinations 1 2)
> ((1 1) (0 1) (1 0) (0 0))
>
> CL-USER > (combinations 1 3)
> ((1 1 1) (0 1 1) (1 0 1) (0 0 1) (1 1 0) (0 1 0) (1 0 0) (0 0 0))
>
> CL-USER > (combinations 2 2)
> ((2 2) (1 2) (0 2) (2 1) (1 1) (0 1) (2 0) (1 0) (0 0))
>
> If you are searching for a general list comprehension algorithm, macros
> could be useful, like described in a talk at the ILC2007:
>
> http://www.international-lisp-conference.org/2007/speakers#latendress...
>
> The talk was very interesting and it was impressive how small the macro was
> to implement list comprehensions like used e.g. in Haskell.
> (combinations 2 2) would look like this:
> ((list x y) (x <- '(0 1 2)) (y <- '(0 1 2)))
>
> I have the paper in printed form (they gave all ILC participants a nice
> book with all articles), maybe it is available on the web, too.
>
> --
> Frank Buss, ····@frank-buss.dehttp://www.frank-buss.de,http://www.it4-systems.de
Frank Buss wrote:
> If you are searching for a general list comprehension algorithm, macros
> could be useful, like described in a talk at the ILC2007:
>
> http://www.international-lisp-conference.org/2007/speakers#latendresse_mario
>
> The talk was very interesting and it was impressive how small the macro was
> to implement list comprehensions like used e.g. in Haskell.
> (combinations 2 2) would look like this:
> ((list x y) (x <- '(0 1 2)) (y <- '(0 1 2)))
>
> I have the paper in printed form (they gave all ILC participants a nice
> book with all articles), maybe it is available on the web, too.
I found it, really interesting:
http://www.iro.umontreal.ca/~latendre/publications/listCompFinal.pdf
--
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de