(amb 'list)

Q&A's, tips, howto's
Locked
Fritz
Posts: 66
Joined: Sun Sep 27, 2009 12:08 am
Location: Russia

(amb 'list)

Post by Fritz »

Trying to select a random value from the list.

Code: Select all

(set 'mylist '((0 0) '(0 1) '(0 2)))
(amb mylist)
Result: ((0 0) (0 1) (0 2))

Different combinations of expand, eval, map and apply wont work in my hands. Only working method I have found is the monstercode with eval-string:

Code: Select all

(eval-string (append "(amb '" (join (map string mylist) " '") ")"))
But there should be some simple solution, right?

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Re: (amb 'list)

Post by m i c h a e l »

Hi Fritz,

Try this:

Code: Select all

> (set 'mylist '((0 0) (0 1) (0 2)))
((0 0) (0 1) (0 2))
> (apply amb mylist)
(0 2)
> (apply amb mylist)
(0 0)
> (apply amb mylist)
(0 2)
> _
Also notice that only one quote is necessary in creating the list. Hope that helps!

m i c h a e l

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: (amb 'list)

Post by cormullion »

m i c h a e l beat me to it...

amb doesn't take a list, just a series of expressions, so:

Code: Select all

apply amb my-list
is what you want

Fritz
Posts: 66
Joined: Sun Sep 27, 2009 12:08 am
Location: Russia

Re: (amb 'list)

Post by Fritz »

Oh, thanx!

Locked