Jump to content

rnd-pick variant that leaves selection unchanged


Recommended Posts

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

Link to comment
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

Link to comment
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

Link to comment
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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

Terms of Use Privacy Policy