I saw a post on comp.lang.lisp demonstrating the suitability of Common Lisp for functional programming. The poster asked to see versions in other languages including Ruby, so I thought I’d whip something up. Here’s the original post with description of the problem:
This one was too much fun for words in re how cool it is programming
with Lisp. I would like to see this in Ruby, Clojure, Qi, and
Scheme. The precise fun part tho is typing it all in in the final form
versus dividing the thing up into steps to get intermediate results,
ie, a test of one's mastery of one's language. Non-functional
languages I guess have no choice but to stop and assign temporaries.
Given:
(defparameter *pets*
'((dog ((blab 12)(glab 17)(cbret 82)(dober 42)(gshep 25)))
(cat ((pers 22)(siam 7)(tibet 52)(russ 92)(meow 35)))
(snake ((garter 10)(cobra 37)(python 77)(adder 24)(rattle 40)))
(cow ((jersey 200)(heiffer 300)(moo 400)))))
Write:
(defun digest-tag-population (tag-population pick-tags count)...)
Such that:
(digest-tag-population *pets* '(dog cat snake) 5)
=> ((DOG CBRET 82) (DOG DOBER 42) (CAT RUSS 92) (CAT TIBET 52) (SNAKE
PYTHON 77))
...the rules being:
- consider only the populations of tags (the first symbol in each
sublist) found in the parameter pick-tags, a list
- take only the most populous of the union of the populations
- return (tag name population) of the most populous in this order:
firstly, by position of the tag in pick-tags
second, ie within a tag, in descending order of population
(defun subseq-ex (st e s)
(subseq s st (min e (length s))))
(defun digest-tag-population (tag-population pick-tags count)
(flet ((tagpos (tag) (position tag pick-tags)))
(stable-sort (subseq-ex 0 count
(sort (loop for (tag population) in tag-population
when (tagpos tag)
append (loop for pop in population
collecting (list* tag pop)))
'> :key (lambda (x)
(caddr x))))
'< :key (lambda (x) (tagpos (car x))))))
(defparameter *pets*
'((dog ((blab 12)(glab 17)(cbret 82)(dober 42)(gshep 25)))
(cat ((pers 22)(siam 7)(tibet 52)(russ 92)(meow 35)))
(snake ((garter 10)(cobra 37)(python 77)(adder 24)(rattle 40)))
(cow ((jersey 200)(heiffer 300)(moo 400)))))
#+test
(digest-tag-population *pets* '(dog cat snake) 5)
And here is my Ruby version:
PETS = [
[:dog, [[:blab, 12], [:glab, 17], [:cbret, 82], [:dober, 42], [:gshep, 25]]],
[:cat, [[:pers, 22], [:siam, 7], [:tibet, 52], [:russ, 92], [:meow, 35]]],
[:snake, [[:garter, 10], [:cobra, 37], [:python, 77], [:adder, 24], [:rattle, 40]]],
[:cow, [[:jersey, 200], [:heiffer, 300], [:moo, 400]]]
]
def digest_tag_population tag_population, pick_tags, count
tag_population.select {|e| pick_tags.include?(e[0]) }.
inject([]) {|memo,obj| obj[1].each {|e| memo << [obj[0], e[0], e[1]] }; memo }.
sort {|a,b| b[2] <=> a[2] }[0,count].
sort_by {|e| [ tag_population.map{|p| p[0]}.rindex(e[0]), e[2] * -1] }
end
digest_tag_population(PETS, [:dog, :cat, :snake], 5)
Within the function:
Line 1: select elements that match the pick tags
Line 2: map to a list of tuples of the form [:dog, :blab, 12]
Line 3: sort the list of tuples by population and select the first count of them
Line 4: sort by tag position, population
Output:
[[:dog, :cbret, 82],
[:dog, :dober, 42],
[:cat, :russ, 92],
[:cat, :tibet, 52],
[:snake, :python, 77]]
I think Ruby compares very favorably. What do you think? Feel free to submit a version in another language.