Saturday, June 6, 2009

NewLisp doesn't have Loop?

Damn! It's like the uber macro! Even non lispers know about loop in all its non lispy complexity...

Anyway, I was trying to comma separate the list (and maybe along the way fix the nil problem - although I had actually thought lists always had an empty list in the cons cell - maybe I'm crazy):

I think I'll change convention here, too. Should be pretty clear when I'm writing lisp and when I'm commenting in English so I'm going to drop the ;;comment notation.

Here's a first approximation, after ten minutes hacking. I already know what my initial problem is going to be, by the way. It's not that there are too many parens, there are about the right amount. I just can't put them in the right goddamn place. My eyes sees absolutely nothing wrong with:
(list first(lst) str rest(lst))
and maybe if you're not a lisper yours doesn't either. But that's not how you invoke functions in lispland. It should be:
(list (first lst) str (rest lst))
which actually looks nicer now that it's there. It's just that the first doesn't leap out at me yet. So anyway, here's my first take on join. Fairly erlang influenced in terms of 'just cram all the symbols together and they'll be flattened on output:

(define (joinString str lst)
(let (acc) '())
(doJoin acc str lst))
(define (doJoin acc str lst)
(if(= lst '())
acc
(doJoin (list acc str (first lst)) str (rest lst))))

Which reminds me! I should flatten it!

Actually, I just found the final answer:
> (join (map string (map fib (clean nil?(range 1 10)))) ",")
"1,1,2,3,5,8,13,21,34,55"

Sorry to ruin the suspence, but you can't ignore library functions when they're already there...

Here's a better answer, where the nil cleaning has been rolled into range itself via an auxiliary function:
>(join (map string (map fib (range 1 10))) ",")

Actually, here's one better still, which enhances the library 'join' function:

(define (joinString str lst)
(join (map string lst) (string str)))

resulting in:
>(joinString ', (map fib(range 1 10)))

And that one I'm happy with.

Still, I wish I knew why that damned nil keeps getting in there. If this were CL I understand that it would be the list terminator. But NewLisp says it isn't. Obviously there's something wrong with my logic - but that is a matter for tomorrow. Today I'm going to call a success, even though I eventually did have to trawl the API looking for things like (first aList) and (last aList). Comma separated fib list was produced, and now will never be referenced again :)

No comments:

Post a Comment