Length
June 29, 2018
Recently, at a beginning programmer’s forum, I saw a user asking about the function length that finds the length of a list. He gave a simple version of length, then used it find the lengths of two lists and determine which was shorter. He correctly realized that calculating the full lengths of both lists is inefficient if one list is much longer than the other, and he asked if there was a way to run through the lists simultaneously, stopping as soon as it is known which list is shorter.
Your task is to write length and a function to determine which of two lists is shorter; your solution must use recursion. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
language like python tracks the length of list, so the get the length of list is constant time.
Here is a solution is standard Scheme that is easy to read as a
logical expression.
(import (scheme base) (scheme write)) (define (shorter? lst1 lst2) (and (pair? lst2) (or (null? lst1) (shorter? (cdr lst1) (cdr lst2))))) (display (map shorter? '(() () (1) (1) (1 2) (1 2)) '(() (1) () (1 2) (1) (1 2)))) (newline) (#f #t #f #t #f #f)Here’s a solution in racket. The output is:
0 ==> The 1st list is shortest
1 ==> The 2nd list is shortest
2 ==> Both lists are of equal length
(define (shortest-list ls1 ls2)
(let check ((x ls1) (y ls2) (len 0))
(cond
((and (empty? x) (empty? y)) 2)
((empty? x) 0)
((empty? y) 1)
(else (check (cdr x) (cdr y) (add1 len))))))
@echo, lists in Python are arrays, not linked-lists.
https://docs.python.org/3/faq/design.html#how-are-lists-implemented
Here’s a solution in Common Lisp. The shortest function returns 0 if both lists are the same size, 1 if the first list is shortest, and 2 if the second list is shortest.
(defun len (lst) (labels ((aux (lst n) (if (null lst) n (aux (cdr lst) (1+ n))))) (aux lst 0))) (defun shortest (lst1 lst2) (cond ((and (null lst1) (null lst2)) 0) ((null lst1) 1) ((null lst2) 2) (t (shortest (cdr lst1) (cdr lst2)))))Example:
Here’s a solution in Haskell.
Example:
Klong version
ln::{ln2::{:[x~[]; y; .f(1_x; y+1)]}; ln2(x; 0)} :monad shorter::{[l2]; l2::y; :[ln(x)=0; :[ln(y)=0; "Equal"; "1st"]; :[ln(y)=0; "2nd"; ls(1_x; 1_y)]]} :dyadshorter([]; [])
“Equal”
shorter([]; [1])
“1st”
shorter([1]; [])
“2nd”
shorter([1 2]; [3])
“2nd”
shorter([1 2]; [1 2 3])
“1st”
shorter([1]; [1])
“Equal”
Klong version (corrected)
ln::{ln2::{:[x~[]; y; .f(1_x; y+1)]}; ln2(x; 0)}
:monad
shorter::{:[0=ln(x)+ln(y); "Equal" :|ln(y)=0; "2nd" :| ln(x)=0; "First"; shorter(1_x; 1_y)]}
:dyad
Example:
shorter([]; []) "Equal" shorter([]; [1]) "First" shorter([1]; []) "2nd" shorter([1]; [2 3]) "First" shorter([2 3]; [1]) "2nd" shorter([2 3]; [1 2]) "Equal"Another Haskell solution, which is similar to Daniel’s. The main difference is that I’ve golfed it slightly to eliminate one case (at the expense of some symmetry in the arguments).
A good return type for the Haskell solutions re: length comparison is
Ordering, which allows you to return LT, GT, or EQ values.A solution in Racket.
An answer in C#
void Main() { var empty = new int[0]; var five = new[] { 1, 2, 3, 4, 5 }; var four = new[] { 1, 2, 3, 4 }; IsShorterThan(five,four).Dump(); IsShorterThan(four, five).Dump(); IsShorterThan(four, four).Dump(); IsShorterThan(empty, four).Dump(); IsShorterThan(four, empty).Dump(); } bool IsShorterThan(IEnumerable self, IEnumerable other) { var selfHead = self.Cast<object>().FirstOrDefault(); var otherHead = other.Cast<object>().FirstOrDefault(); if(selfHead == null || otherHead == null) { return selfHead == null && otherHead != null; } return IsShorterThan(self.Cast<object>().Skip(1),other.Cast<object>().Skip(1)); }@TomParish, it looks like your solution is simulating a linked list with an array.
Here’s a C# solution that uses a linked list directly. Unlike my earlier solutions, there is no attempt to make the length function tail recursive since this won’t be optimized in C#.
using System; using System.Text; namespace cs { class Node<T> { public T value; public Node<T> next; override public string ToString() { StringBuilder sb = new StringBuilder(); sb.Append("["); Node<T> node = this; while (node != null) { sb.Append(node.value); node = node.next; if (node != null) sb.Append(", "); } sb.Append("]"); return sb.ToString(); } } class Program { static int Length<T>(Node<T> list) { if (list == null) { return 0; } else { return 1 + Length(list.next); } } // Returns -1 if list1 shorter than list2, // 0 if list1 and list2 are the same length, // or 1 if list1 longer than list2. static int Compare<T>( Node<T> list1, Node<T> list2) { if (list1 == null && list2 == null) { return 0; } else if (list1 == null) { return -1; } else if (list2 == null) { return 1; } else { return Compare(list1.next, list2.next); } } static void Main(string[] args) { Node<int> list1 = null; for (int i = 7; i > 0; --i) { Node<int> next = list1; list1 = new Node<int>(); list1.value = i; list1.next = next; } Node<int> list2 = null; for (int i = 9; i > 0; --i) { Node<int> next = list2; list2 = new Node<int>(); list2.value = i; list2.next = next; } Console.WriteLine("list1: " + list1); Console.WriteLine("list2: " + list2); Console.WriteLine("Length(list1): " + Length(list1)); Console.WriteLine("Length(list2): " + Length(list2)); Console.WriteLine("Compare(list1,list2): " + Compare(list1, list2)); } } }Output: