Zark Newsletter Extracts: Limbering Up: Combinations
Vector 17:4, page 26
Transcribed from the printed issue; not yet reviewed. Read it in the PDF of the issue, page 26
Limbering Up: Combinations¶
(The purpose of this column is to work some flab off your APL midsection. Like muscles, your APL skills can atrophy if not exercised with adequate frequency and variety. This column presents a task for you to perform. Set aside a few minutes from your busy schedule and work the task. Mail in your solution and stay tuned for the results.)
The combination function ! is one you probably don't use often. It tells you the number of different ways you can select M coloured jelly beans from a set of N jelly beans. (And it even works o other objects!) Each jelly bean has a different colour.
For example, suppose you have 5 beans, coloured Black, Green, Purple, Red, and Yellow. How many different combinations can you make?
Ten combinations. Here they are:
Your task is to write a function that allows you to construct a set of different combinations. Call it COMBIN, and give it the same syntax as the ! function. Given the left argument M and the right argument N, it returns a matrix of indices with MIN rows and M columns.
To get the answer to the jelly bean problem, use the expression,
Seem simple enough? Try your hand at writing this function. Naturally, efficiency is important. However, elegance (whatever that is) and originality will also be acknowledged.
Reprinted with kind permission from Zark APL Tutor News, a quarterly publication of Zark Incorporated, 23 Ketchbrook Lane, Ellington, CT06029, USA