Arc Forumnew | comments | leaders | submitlogin
1 point by bOR_ 5820 days ago | link | parent

Different problem, similar question.

rand-choice returns one of its arguments. Can it also pick one of the arguments in a list?

  (rand-choice (list 1 2 3)) 
gives (as per expected)

  (1 2 3)
and to pick a random value of a list I currently use

  (let mylist (list 1 2 3)
     (mylist (rand(len mylist))))
Scanned through arcfn.com, but didn't spot anything that turns a list into a number of arguments, so that it can be fed to rand-choice

  (rand-choice (delist mylist))


2 points by almkglor 5820 days ago | link

'apply

  (apply rand-choice (list 1 2 3))

-----

2 points by bOR_ 5820 days ago | link

Had tried apply, but that doesn't work in arc2. I get a

  "Function call on inappropriate object #3(tagged mac #<procedure>) (1 2 3)"
Might be inevitable for me to move over towards anarki, if it works there. :P

-----

3 points by absz 5820 days ago | link

Actually, in looking over the functions again, it turns out that there is already a function that does what you want: random-elt. That should do what you need it to do.

As for your problem (and no, it doesn't work on Anarki[1]), the problem is that rand-choice is a macro, not a function, and apply only works on functions. The only solution is a terrible hack:

  (def mapply (macro . parms)
    " Applies the macro `macro' to `parms', which are not quoted.
      See also [[mac]] [[apply]] "
    (eval:apply (rep macro) (join (butlast parms) (last parms))))
But don't use that!

[1]: But you should use Anarki anyway :)

-----

2 points by bOR_ 5820 days ago | link

Thanks for the reminder not to apply on macro's, and for finding random-elt. I need to learn how to read ;).

random-elt does exactly what I want.

(btw.. small consistency issue in the syntax here?) rand-choice random-elt

-----

1 point by absz 5820 days ago | link

To further clarify, if you've programmed in Ruby or Python: (apply func ... arglist) is equivalent to func(..., * arglist) in those languages.

-----