Sign in to follow this  
Followers 0
torstenanders

rnd-pick variant that leaves selection unchanged

9 posts in this topic

I would like to randomly pick a nested list. That works in principle, but the nesting is lost.

(rnd-pick '(((h q) (h))
            ((h. q) (h))
            ((h. h) (h))))
=> ((3/4 1/4) (1/2)) ; instead returns (3/4 1/4 1/2)

I guess I have to roll my own :)

(defun rnd-pick2 (selections &key (prob 0.5) seed)
  "Randomly selects a value from `selections' (without transforming that value).
  Very similar to rnd-pick, but leaves selected value unchanged (e.g., unflattened).

  Args
  prob: a floating-point number. Probability value. The default is 0.5.

  Example
  (rnd-pick2 '(((h q) (h))
               ((h. q) (h))
               ((h. h) (h))))
  => ((h. q) (h))  
  "
  (rnd-seed seed)
  (nth (rnd1 :low 0 :high (1- (length selections)) :prob prob :seed (seed)) 
       selections))

Best,

Torsten

AM likes this

Share this post


Link to post
Share on other sites
(rnd-pick '(((h q) (h))
            ((h. q) (h))
            ((h. h) (h))) :encode nil)

=> ((h. q) (h))

 

It works this way but i think it need to work also with :encode T. May be a bug in encoding process who remove nested list. Janusz ?

 

 

S.

Share this post


Link to post
Share on other sites

Thanks. Unfortunately, that feature is not documented and therefore effectively does not exist for users.

 

Janusz, I understand that writing documentation takes time that you do not have, but at least please make sure that the purpose of every function argument is briefly mentioned in the documentation. Otherwise, why would you program such features, if no-one can use them :)  In order to help for this function, here is a suggestion.

 

Quote

With :encode T (the default) the function flattens out sublists and transforms OMN symbols (e.g., lengths symbols are turned into ratios), while :encode NIL returns selections unchanged.

 

BTW: :encode T does indeed work that way for me (latest version, 1.2.21969M), contrary to what Stephane reports. 

 

Best,

Torsten

Share this post


Link to post
Share on other sites

Ah, brilliant. Sorry, I did not realise that this was an argument shared by many functions (like section, seed, or flat), which is therefore not documented. Just noticed the function encode-seq -- is that related to this argument? I understand that this function only removes repeat symbols, which I agree would be useful to do by default.

 

However, encode-seq does not flatten the given lists, while rnd-pick does. That was my actual stumbling block here.

 

(encode-seq '((h. q) (h)))

=> ((3/4 1/4) (1/2))

 

(rnd-pick '(((h q) (h))
            ((h. q) (h))
            ((h. h) (h))))

=> (3/4 1/4 1/2)

 

Does it really make sense to flatten the output of rnd-pick by default, or is that perhaps a mistake?

 

Thanks again!

 

Best,

Torsten

Share this post


Link to post
Share on other sites

Sorry, you seemingly just misread my question :wink:  These double nested lists are single-nested alternatives for rnd-pick, but rnd-pick by default removes the nesting.

 

(rnd-pick '(((h q) (h))
            ((h. q) (h))
            ((h. h) (h))))

=> (3/4 1/4 1/2)

 

Stephane pointed out that this can be avoided by setting the argument :encode to nil, but if others later run into this problem again this is still not explained in the documentation.

 

I would argue that the default behaviour should be to preserve the (single) nesting, should it not?

 

> In OM we don't use double nested list.

> what is the purpose of this.

 

This was not my question here, but I actually do use further nesting levels occasionally to represent additional information for intermediate processing, e.g., for representing sections :mmm:

 

Best,

Torsten

Share this post


Link to post
Share on other sites

This is not a trivial thing to do because the function has to deal with so many cases.
Please give me a REAL example of the use of double nested lists, especially with omn symbols.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0