# Random Group Assignments

Posted on October 13, 2013 by Brian Jaress

This small script randomly assigns members to groups of (as much as possible) equal size. I wrote it for Katie’s weekend “coffee talks” where her students practice English with a native speaker.

Recently, I decided1 to write my personal scripts in Haskell. I’m still a Haskell beginner, so even small scripts like this one are learning experiences.

## Example Run

Groups are command-line arguments, and members are passed through stdin:

# randgroups Katie Brian James
Hiro, Kazumi, Yoko M, Keiko,
Kazuko, Ritsuko, Hide, Miki

Possible output:

Brian: Hide, Keiko, Kazuko
James: Hiro, Kazumi
Katie: Ritsuko, Miki, Yoko M

## Libraries

Most work is handled by libraries. The randomness comes from the random-shuffle package, based on code by Oleg Kiselyov. Also, since Haskell isn’t tailored toward string manipulation, we need a fair number of imports for that.2

## Core Logic

The members are shuffled into a random order, then assigned round-robin to the groups. Because of that, input order of the groups determines which groups are larger (if they cannot be exactly the same).

The split library is based on function composition, an approach that ends up similar to method chaining but with the order reversed.3

## Output

The assignment above creates a list of pairs, each member paired with its group. The gather function brings together members assigned to the same group, so that each group becomes paired with its full list of members.

After gather, the data’s structure matches the output’s structure, but punctuation is needed.

\begin{code} prettyPrint :: [(String, [String])] -> IO () prettyPrint = mapM_ (putStrLn . format) where format (group, members) = group ++ “:” ++ intercalate “,” members \end{code}

Possible output:

Group A: Donnie, Lucinda, Ashley K., Christopher R., Marcella
Group B: Hiram, Christopher S., Nikki, Michael
Group C: Alexander, Ashley J., Dominick, Ava

## Final Thoughts

Most of the work was looking up libraries in the documentation. In fact, the most challenging part was navigating documentation that is organized according to the rules of Haskell.

For example, when a library function returns an unfamiliar type class, information on how to use it might be in the list of known implementations, rather than the type class definition. That’s because Haskell can allow the caller to choose the type of the result. Some type classes only define creation functions and rely on each type to define its own way of accessing the result.

Overall, I enjoyed writing this, and it’s useful to me. Haskell was surprisingly suitable for a small text-processing script.

1. Mainly because of the HsShellScript library, though I didn’t need it for this script.

2. Haskell needs a “quick start” library, with definitions like join = intercalate and trim = unpack . strip . pack.

3. A method chaining version of split . dropDelims . dropBlanks . oneOf \$ ",\n" in a language like Java might be oneOf(",\n") .dropBlanks() .dropDelims() .split(string).