tickling lisp
Henry Spencer
henry-lqW1N6Cllo0sV2N9l4h3zg at public.gmane.org
Mon Oct 20 22:40:28 UTC 2003
On Tue, 21 Oct 2003, Peter L. Peres wrote:
> It turns out it is *very* hard to make a test in tcl that decides whether
> "{a}" and "a" are the same.
For all practical purposes, they *are* the same. Tcl makes no distinction
between an atom and a one-element list. This program:
set out [list] ;# an empty list
foreach x {a {b} c} {
lappend out $x
}
puts "$out"
prints
a b c
What you care about is whether the sub-list has multiple atoms in it. To
go arbitrarily deep, you have to distinguish between trivial (length=1)
lists and non-trivial ones, and treat them differently. So:
proc flat {in} {
set out [list]
foreach x $in {
if {[llength $x] > 1} {
set out [concat $out [flat $x]]
} else {
lappend out $x
}
}
return $out
}
flat {a {b} {c {d e}} f}
prints
a b c d e f
Yes, it's a bit weird. Takes a while to wrap your mind around it.
Henry Spencer
henry-lqW1N6Cllo0sV2N9l4h3zg at public.gmane.org
--
The Toronto Linux Users Group. Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml
More information about the Legacy
mailing list