Order two numerical lists: Difference between revisions

From Rosetta Code
Content added Content deleted
(i wonder which languages have a more concise way of doing this)
 
(→‎Tcl: Added implementation)
Line 24: Line 24:
return order_array(a[1..], b[1..]);
return order_array(a[1..], b[1..]);
return a[0] < b[0];
return a[0] < b[0];
}</lang>

=={{header|Tcl}}==
<lang tcl>proc numlist< {A B} {
foreach a $A b $B {
if {$a<$b} {
return 1
} elseif {$a>$b} {
return 0
}
}
return 0
}</lang>
}</lang>

Revision as of 14:33, 28 November 2011

Order two numerical lists is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Write function that orders two lists or arrays filled with numbers. The function should accept two lists as arguments and return true if the first list should be ordered before the second, and false otherwise.

The order is determined by comparing the first element of each list. If the first elements are equal, then the second elements should be compared, and so on, until one of the list has no more elements.

If the first list runs out of elements the result is true. false otherwise.

Common Lisp

<lang Lisp>(defun list< (a b)

 (cond ((not a) t)
       ((not b) nil)
       ((= (first a) (first b))
        (list< (rest a) (rest b)))
       (t (< (first a) (first b)))))</lang>

Pike

<lang Pike>int(0..1) order_array(array a, array b) {

 if (!sizeof(a)) return true;
 if (!sizeof(b)) return false;
 if (a[0] == b[0])
   return order_array(a[1..], b[1..]);
 return a[0] < b[0];

}</lang>

Tcl

<lang tcl>proc numlist< {A B} {

   foreach a $A b $B {
       if {$a<$b} {
           return 1
       } elseif {$a>$b} {
           return 0
       }
   }
   return 0

}</lang>