Another challenge

Q&A's, tips, howto's
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Another challenge

Post by cormullion »

I thought this thread on comp.lang.lisp was quite entertaining https://groups.google.com/d/topic/comp. ... discussion. It's to produce the list:

Code: Select all

((1 "a") (1 "b") (1 "c") (1 "d") (2 "a") (2 "b") (2 "c") (2 "d") (3 "a")
 (3 "b") (3 "c") (3 "d") (4 "a") (4 "b") (4 "c") (4 "d") (5 "a") (5
"b") (5 "c") (5 "d"))


Can you provide a newLISP alternative to the various loops, mapcans, collects, iters, and nconcs? Can you avoid creating intermediate storage? Can you do all this while resisting the urge to insult other programmers?! :)

Obviously, there's a simple iterator solution, using two dolists. I found a shortcut in newLISP. Can you find it?

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Re: Another challenge

Post by Sammo »

This works but is ugly looking.

Code: Select all

(map list (sort (flat (dup (sequence 1 5) 4))) (explode (dup "abcd" 5)))
With comments:

Code: Select all

(map list
	(sort (flat (dup (sequence 1 5) 4))) ;numbers
	(explode (dup "abcd" 5)) ;letters
)
Edit: Simplified expression generating list of letters. Was "(flat (dup (explode "abcd") 5))."
Last edited by Sammo on Thu Mar 10, 2011 5:03 am, edited 2 times in total.

johu
Posts: 143
Joined: Mon Feb 08, 2010 8:47 am

Re: Another challenge

Post by johu »

A shortcut in newLISP ?
Though my understanding might be different, I try :

Code: Select all

(for (i 1 5) (extend x (map (curry list i) '("a" "b" "c" "d"))))
or

Code: Select all

(for (i 1 5) (extend '() (map (curry list i) '("a" "b" "c" "d"))))

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

And Another challenge

Post by newdep »

Just to keep that head fresh..Explain the behavior...

Code: Select all

(((lambda(x) (lambda(x)))))   ---> nil
((lambda(x) (lambda(x))))   ---> (lambda (x))
(lambda(x) (lambda(x)))   ---> (lambda (x) (lambda (x)))
((lambda(x) lambda(x)))   ---> ERR: invalid lambda expression : (lambda x)
-- (define? (Cornflakes))

Locked