From: Joe English on 21 Apr 2010 19:56 Glenn Jackman wrote: > Donald G Porter wrote: >> The [lsort -dictionary] command could also be the core of >> a pairwise comparison function, but I'll leave it to other >> to test whether the performance of such a thing is acceptable >> and/or better than other alternatives. > > Here's an implementation > > proc string_comp_dictionary {s1 s2} { > if {$s1 eq $s2} { > return 0 > } else { > set l [list $s1 $s2] > if {$l == [lsort -dictionary $l]} { > return -1 > } else { > return 1 > } > } > } Here's what I use when I need this: proc dictionary_compare {a b} { lindex [lsort -dictionary -index 0 [list [list $a -1] [list $b 1]]] 0 1 } [ And then I always grumble about the unholy mess of options flags and feechurs that is the [lsort] command and wish that Tcl just provided [dictionary_compare] as a standalone primitive instead. But that's a different problem. ] > Is that the best way to examine list equality? I also considered > examining them as strings: > > {"$s1 $s2" eq [join [lsort -dictionary [list $s1 $s2]]]} I wouldn't really use either one of those. "List equality" isn't really well-defined -- you need to first decide "lists of *what*?" Comparing two lists of numbers is a different function than comparing two lists of strings, or two lists of dictionaries, or two lists of.... It usually comes down to [all [zipWith $predicate $l1 $l2]], where $predicate is a comparison function appropriate to the type of the elements of the list. --Joe English
First
|
Prev
|
Pages: 1 2 3 4 5 Prev: using libusb from TCL Next: TclOO: Renaming "new" and "create" methods? |