Post Reply 
Programming puzzles: processing lists!
06-19-2017, 05:47 AM (This post was last modified: 06-19-2017 05:49 AM by Werner.)
Post: #169
RE: Programming puzzles: processing lists!
DOCOMB
In: 3: List
2: n
1: ob
Out: result_list. <ob> is evaluated for every combination of n elements taken from the list. The results are wrapped up in result_list.

Code:
\<<
  ROT 0
  \-> ob L DoC
  \<<
    DEPTH DEPTH ROLLD
    L SIZE
    {}
    { 3 PICK 1 -
      \-> t n
      \<<
        IF n
        THEN FOR s n s 1 - L s GET 1 \->LIST t + DoC EVAL NEXT 
        ELSE FOR s L s GET t LIST\-> DROP ob EVAL NEXT
        END
      \>>
    } DUP 'DoC' STO EVAL
    DEPTH DEPTH ROLL -
    IF DUP 0 > THEN \->LIST ELSE DROP END
  \>>
\>>

eg.

3: { 1 2 3 4 }
2: 3
1: \<< 3 \->LIST \>>
DOCOMB

will result in

{{ 1 2 3 } { 1 2 4 } { 1 3 4 } { 2 3 4 }}

Cheers, Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Programming puzzles: processing lists! - Werner - 06-19-2017 05:47 AM



User(s) browsing this thread: 1 Guest(s)