Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

/prog/ Challenge #9002

Name: Anonymous 2011-05-02 20:12

The task:

Print strings from "a" to "zzzzz" without using any loop or conditional statements. Don't just write all 1000 permutations out by hand. The output should look like this:
a
b
c
...
aa
ab
ac
...
zzzzx
zzzzy
zzzzz


inb4 lipthfags and dead dogs using some obscure functionality of their obscure languages.

Name: Anonymous 2011-05-02 21:52

>>8
[a++b++c++d++e|a<-f,b<-f,c<-f,d<-f,e<-f]
Which is just syntactic sugar for

do a <- f
   b <- f
   c <- f
   d <- f
   e <- f
   return a++b++c++d++e


Which is just syntactic sugar for
f >>= \a -> f >>= \b -> f >>= \c -> f >>= \d -> f >>= \e -> return a++b++c++d++e

Which, for the list monad, is equivalent to
concatMap (\a -> concatMap (\b -> concatMap (\c -> concatMap (\d -> concatMap (\e -> [a++b++c++d++e]) f) f) f) f) f)

Which is equivalent to
concat $ map (\a -> concat $ map (\b -> concat $ map (\c -> concat $ map (\d -> concat $ map (\e -> [a++b++c++d++e]) f) f) f) f)

Where concat is

concat [] ys = ys
concat [x:xs] ys = x : (concat xs ys)

and map is

map _ [] = []
map f [x:xs] = (f x) : (map f xs)


Both are clearly loops.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List