This program in APL (or J, or...)?

6 views
Skip to first unread message

pineapple

unread,
Nov 10, 2008, 1:46:49 PM11/10/08
to
Knowing the reputation APL, J, and others have for being able to do an
awful lot in a small amount of code, I was curious what a solution
would look like for this "challenge" I ran across.

A Logo program, "choices":

###
to choices :menu
foreach crossmap "sentence :menu "print
end
###

Invoke the program like this:

###
choices [[small medium large]
[vanilla [ultra chocolate] lychee [rum raisin] ginger]
[cone cup]]
###

The program responds with:

###
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
###

If you want to see solutions posted in various languages, go here:

http://lojic.com/blog/2007/08/31/logo-ruby-javascript/

Stevan Apter

unread,
Nov 10, 2008, 3:13:29 PM11/10/08
to
choices:(,/,/:\:)/

choices(`small`medium`large;`vanilla`ultra_chocolate`lychee`rum_raisin`ginger
(`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)

"pineapple" <pineapp...@yahoo.com> wrote in message news:a3076ded-d9b9-49e5...@a29g2000pra.googlegroups.com...

Phil Last

unread,
Nov 10, 2008, 3:45:36 PM11/10/08
to
↑,⊃∘.{⍺,' ',⍵}/('small' 'medium' 'large')('vanilla' 'ultra
chocolate' 'lychee' 'rum raisin' 'ginger')('cone' 'cup')

Ric

unread,
Nov 10, 2008, 5:21:25 PM11/10/08
to
In J:
>,{ ('small';'medium';'large');('vanilla';'ultra
chocolate';'lychee';'rum raisin';'ginger');<('cone';'cup')
+------+---------------+----+
|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 |
+------+---------------+----+

choices=: >@,@{
format=: ([: }.@; ' ' ,&.> ])"1
format choices ('small';'medium';'large');('vanilla';'ultra
chocolate';'lychee';'rum raisin';'ginger');<('cone';'cup')

Ric

unread,
Nov 10, 2008, 5:45:34 PM11/10/08
to
On Nov 11, 11:21 am, Ric wrote:
> In J:

>    choices=: >@,@{
>    format=: ([: }.@; ' ' ,&.> ])"1
>    format choices ('small';'medium';'large');('vanilla';'ultra
> chocolate';'lychee';'rum raisin';'ginger');<('cone';'cup')

A slightly simpler version of format:
format=: ((<' ') }.@;@,. ])"1

Of course there is no need to reinvent the wheel for format. You could
use existing utilities from the strings script, or the tables/dsv
addon.
load 'strings tables/dsv'
arg=: ('small';'medium';'large');('vanilla';'ultra


chocolate';'lychee';'rum raisin';'ginger');<('cone';'cup')

' 'joinstring"1 >,{ arg
or
' 'makedsv >,{ arg

Martin Neitzel

unread,
Nov 10, 2008, 4:31:56 PM11/10/08
to
[J solution]

>choices [[small medium large]
> [vanilla [ultra chocolate] lychee [rum raisin] ginger]
> [cone cup]]

Your encoding appears to be a bit inconsistent: inner inner list
seem to encode mutli-word strings instead of alternives, if I look
at your requested output. I'll take liberty to simplify this aspect;
in order to save columns and calories, I'll also drop the ultra choc:

]options =. ;:each 'sml med lrg';'van lyc rum_raisin ginger';'cone cup' +-------------+---------------------------+----------+
|+---+---+---+|+---+---+----------+------+|+----+---+|
||sml|med|lrg|||van|lyc|rum_raisin|ginger|||cone|cup||
|+---+---+---+|+---+---+----------+------+|+----+---+|
+-------------+---------------------------+----------+

>to choices :menu
> foreach crossmap "sentence :menu "print
>end

The "crossmap" equivalent in J is "{" aka Catalogue:

{ options
+---------------------+--------------------+
|+---+---+----+ |+---+---+---+ |
||sml|van|cone| ||sml|van|cup| |
|+---+---+----+ |+---+---+---+ |
+---------------------+--------------------+
|+---+---+----+ |+---+---+---+ |
||sml|lyc|cone| ||sml|lyc|cup| |
|+---+---+----+ |+---+---+---+ |
+---------------------+--------------------+
...yadda yadda, shape 3 4 2 of length-3 box lists.

The rest just takes care of unpacking the boxes into nice strings:

;:^:_1 > , { options
sml van cone
sml van cup
sml lyc cone
...
lrg rum_raisin cup
lrg ginger cone
lrg ginger cup

Martin Neitzel

Ric

unread,
Nov 10, 2008, 7:40:58 PM11/10/08
to
Martin Neitzel) wrote:
> [J solution]
>    ;:^:_1 > , { options

I like ;:^:_1 !!!
monadic ;: is Words (box a string/sentence into J "words")
^:_1 is inverse so ;:^:_1 is the inverse of Words and unboxes words
into a string/sentence.

pineapple

unread,
Nov 10, 2008, 8:02:46 PM11/10/08
to
Thanks to all (and future) submitters. Incredible!!

pineapple

unread,
Nov 10, 2008, 8:49:07 PM11/10/08
to
On Nov 11, 3:13 am, "Stevan Apter" <encryptednos...@nsl.com> wrote:
> choices:(,/,/:\:)/

Identify this language. Is it K?

Jan Karman

unread,
Nov 13, 2008, 7:34:05 AM11/13/08
to

"pineapple" <pineapp...@yahoo.com> wrote in message
news:d87704b5-b92f-4336...@v16g2000prc.googlegroups.com...

> On Nov 11, 3:13 am, "Stevan Apter" <encryptednos...@nsl.com> wrote:
>> choices:(,/,/:\:)/
>
> Identify this language. Is it K?

reminds me of an example in the VAX APL manual (page 1-178), defined as an
"odometer"
http://www.softwarepreservation.org/projects/apl/book/199106_VAX%20APL%20Reference%20Manual_AA-GV09C-TE.pdf


pineapple

unread,
Nov 13, 2008, 8:21:36 AM11/13/08
to
On Nov 13, 7:34 pm, "Jan Karman" <aq...@planet.nl (remove the q's)>
wrote:

> reminds me of an example in the VAX APL manual (page 1-178), defined as an
> "odometer"http://www.softwarepreservation.org/projects/apl/book/199106_VAX%20AP...

Funny, when I implemented a solution to this off the top of my head, I
modeled an odometer.

Jan Karman

unread,
Nov 13, 2008, 8:32:20 AM11/13/08
to

"pineapple" <pineapp...@yahoo.com> wrote in message
news:76e4360c-aa2c-4bcb...@b31g2000prb.googlegroups.com...

the example is on numbers only,

,/{shoe}{jot}{dot}, /{iota}{each} V (V e.g. 1 2 3)
1 1 1 1 1 2 1 1 3 1 2 1 1 2 2 1 2 3

but I guess on strings it will need a slight modification
jk


Jan Karman

unread,
Nov 13, 2008, 8:57:57 AM11/13/08
to

"Jan Karman" <aq...@planet.nl (remove the q's)> wrote in message news:...

oops!
should be ,{shoe}{jot}{dot},/{iota}{each} V
and {take},{shoe}{jot}{dot},/{iota}{each} V
gives us

Reply all
Reply to author
Forward
0 new messages