Rebol Talk Forum  |  Getting Started  |  Newbie Help  |  Topic: Fun Recursion Example from Logo
Pages: [1] Print
Author Topic: Fun Recursion Example from Logo  (Read 424 times)
Rocky Shoals
Newbie
*
Offline Offline

Posts: 5


View Profile
Fun Recursion Example from Logo
« on: November 29, 2007, 07:17:51 PM »

Taken from http://www.cs.berkeley.edu/~bh/logo-sample.html

Here's a fun one. Given the following block:

block: [
 ["small" "medium" "large"]
 ["vanilla" "ultra chocolate" "lychee" "rum raisin" "ginger"]
 ["cone" "cup"]
]

How would you write a recursive function to create all possible combinations? In other words, it would produce the following output:

small vanilla cone
small vanilla cup
small ultra chocolate cone
small ultra chocolate cup
small lychee cone
small lychee cup
small rum raisin cone
small rum raisin cup
small ginger cone
small ginger cup
medium vanilla cone
medium vanilla cup
medium ultra chocolate cone
medium ultra chocolate cup
medium lychee cone
medium lychee cup
medium rum raisin cone
medium rum raisin cup
medium ginger cone
medium ginger cup
large vanilla cone
large vanilla cup
large ultra chocolate cone
large ultra chocolate cup
large lychee cone
large lychee cup
large rum raisin cone
large rum raisin cup
large ginger cone
large ginger cup

The solution should accept any number of categories and obviously not hard-code the size of the block.
Logged
Gregg
Newbie
*
Offline Offline

Posts: 26


View Profile WWW
Re: Fun Recursion Example from Logo
« Reply #1 on: December 01, 2007, 03:27:29 PM »

If you want a direct translation, it might look something like this:

choices: func [menu sofar] [
    if empty? menu [print reform [sofar] exit]
    foreach item first menu [
        choices next menu reduce [sofar item]
    ]
]

choices [
    [small medium large]
    [vanilla "ultra chocolate" lychee "rum raisin" ginger]
    [cone cup]
] []
Logged
Gregg
Newbie
*
Offline Offline

Posts: 26


View Profile WWW
Re: Fun Recursion Example from Logo
« Reply #2 on: December 01, 2007, 03:35:49 PM »

To make it more interesting (and real world), how would you design a menu system that actually had to deal with combinatorial constraints. For example, maybe you can only get a waffle cone in one size, sugar in two others, and a cup in any size.

I know the example is meant to teach recursion. My thought experiment would be to look at how you might defined and structure your data and how that leads to other approaches (e.g. nested blocks and parse).
Logged
Rocky Shoals
Newbie
*
Offline Offline

Posts: 5


View Profile
Re: Fun Recursion Example from Logo
« Reply #3 on: December 03, 2007, 02:21:32 PM »

Thanks gregg--
I posted this mainly for newbie fun, and because it was germane to some code I'm toying with (exploring the opposite of parse/string decomposition). I doubt you even needed to look at the Logo source.  Grin

Feel free to educate on parse and combinatorial constrants-- I'd sure be interested, although this topic should probably be moved out of the Newbie section so that no-one's head explodes.  Shocked

I'm interested in your combinatorial constraint example in relation to interactive & adaptable REBOL dialects. Here is some academic work that has stimulated my thinking.

Faceted Taxonomy-based Information Management
http://www.dbworldx.di.unito.it/find07/tzitzikas.pdf

Mining the Meaningful Term Conjunctions from Faceted Taxonomies
http://www.ics.forth.gr/isl/publications/paperlink/ExpressionMiningODBASE04.pdf

Best regards
Logged
Pages: [1] Print 
Rebol Talk Forum  |  Getting Started  |  Newbie Help  |  Topic: Fun Recursion Example from Logo
Jump to:  

  
Quick Search...

Advanced search
  
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 17, 2008, 04:58:50 PM
Username: Password: Session Length:
  

News: 01-09-08

Alpha version of REBOL 3 has been released!


  
2169 Posts in 562 Topics by 1224 Members
Latest Member: thyptoste

  Rebol Talk Forum | Powered by SMF 1.0.9.
© 2001-2005, Lewis Media. All Rights Reserved.

RT design by Defiant Pc