Version 1 of list comparison, elementwise

Updated 2009-10-18 13:17:20 by wdb

wdb -- looking for list comparison similar to equal? in Scheme, I found that it is surprisingly difficult due to the lack of types. Here my approach in Tcl. The procedure lequal checks recursively on elementwise equality:

proc lequal {a b} {
  if {![string is list $a]} then {
    string equal $a $b
  } elseif {![string is list $b]} then {
    return 0
  } elseif {[llength $a] != [llength $b]} then {
    return 0
  } elseif {[llength $a] == 0} then {
    return 1
  } elseif {[llength $a] > 1} then {        
    expr {[lequal [lindex $a 0] [lindex $b 0]]] &&
          [lequal [lrange $a 1 end] [lrange $b 1 end]]]}
  } elseif {$a eq [lindex $a 0] || $b eq [lindex $b 0]} then {
    string equal $a $b
  } else {
    lequal [lindex $a 0] [lindex $b 0]
  }
}

Don't know if in real life necessary, but by reasons of completeness, I did it.