Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List length in scheme

Tags:

scheme

Hi I am trying to write a program where given a list of lists check to see if they are equal in size and return #t if they are.

So for example if i were to write (list-counter? '((1 2 3) (4 5 6) (7 8 9))) the program would return #t, and (list-counter? '((1 2 3) (4 5 6) (7 8))) would return #f.

SO far this is what I have done:

 (define list-counter?
  (lambda (x)
    (if (list? x)
        (if (list?(car x))
      (let (l (length (car x))))
        (if (equal? l (length(car x))))
        (list-counter?(cdr x))
 ) ) ) ) ) 

I think where I am going wrong is after I set the length of l to the length of the first list. Any help would be appreciated.

like image 673
gestalt Avatar asked Mar 22 '12 00:03

gestalt


People also ask

What is a list in Scheme?

In contrast to Scheme's unstructured data types, such as symbols and numbers, lists are structures that contain other values as elements. A list is an ordered collection of values. In Scheme, lists can be heterogeneous, in that they may contain different kinds of values.

How do I find the length of a list in R?

To get length of list in R programming, call length() function and pass the list as argument to length function. The length function returns an integer representing the number of items in the list.

What is car and CDR in Scheme?

car is an acronym from the phrase Contents of the Address part of the Register; and cdr is an acronym from the phrase Contents of the Decrement part of the Register. These phrases refer to specific pieces of hardware on the very early computer on which the Lisp language was developed.

What is a predicate in Scheme?

A predicate is a procedure that always returns a boolean value (#t or #f). An equivalence predicate is the computational analogue of a mathematical equivalence relation (it is symmetric, reflexive, and transitive).


1 Answers

There are several ways to solve this problem. For instance, by hand and going step-by-step:

(define (all-lengths lists)
  (if (null? lists)
      '()
      (cons (length (car lists))
            (all-lengths (cdr lists)))))

(define (all-equal? head lengths)
  (if (null? lengths)
      true
      (and (= head (car lengths))
           (all-equal? head (cdr lengths)))))

(define (list-counter? lists)
  (let ((lengths (all-lengths lists)))
    (all-equal? (car lengths) (cdr lengths))))

Let me explain the above procedures. I'm dividing the problem in two steps, first create a new list with the lengths of each sublist - that's what all-lengths does. Then, compare the first element in a list with the rest of the elements, and see if they're all equal - that's what all-equal? does. Finally, list-counter? wraps it all together, calling both of the previous procedures with the right parameters.

Or even simpler (and shorter), by using list procedures (higher-order procedures):

(define (list-counter? lists)
  (apply = (map length lists)))

For understanding the second solution, observe that all-lengths and all-equal? represent special cases of more general procedures. When we need to create a new list with the result of applying a procedure to each of the elements of another list, we use map. And when we need to apply a procedure (= in this case) to all of the elements of a list at the same time, we use apply. And that's exactly what the second version of list-counter? is doing.

like image 156
Óscar López Avatar answered Oct 06 '22 16:10

Óscar López