| I realize there's already a thread for posting snippets of Arc code, but I thought it would be nice to have some language oriented threads. So if you have snippets of Ruby code that you've translated to Arc (one liners or larger), please submit a comment with the Ruby & Arc versions. I'll start with a snippet that began life as Logo on Brian Harvey's home page, and was then translated to 13 or so languages here: http://lojic.com/blog/2007/08/31/logo-ruby-javascript/ In Ruby, it was: def choices menu, sofar=[]
if menu.empty?
puts sofar.join(' ')
else
menu[0].each {|item| choices(menu[1..-1], sofar + [item]) }
end
end
choices [['small', 'medium', 'large'],
['vanilla', 'ultra chocolate', 'lychee', 'rum raisin', 'ginger'],
['cone', 'cup']]
My first draft in Arc is: ; Join elements of lst separated by sep into a string
; (must be a better way to do this)
(def joinstr (lst (o sep " "))
(if lst
(string (car lst) (apply string (map [string sep _] (cdr lst))))
""))
; Cartesian product of elements of menu
(def choices (menu (o result '()))
(if menu
(each x (car menu)
(choices (cdr menu) (cons x result)))
(prn (joinstr:rev result))))
(choices (list
(list "small" "medium" "large")
(list "vanilla" "ultra chocolate" "lychee" "rum raisin" "ginger")
(list "cone" "cup")))
|