Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Scheme Programming Language

Filter longest lists in list help


Hi all,

I am new to Scheme and currently learning to use DrScheme
Now I am playing with lists to get the hang of it.

Ok so what I want to do now is to write a procedure to filter out the
longest lists in a list,
for example
(longest '((1 2 3) (1) (1 2 3 4) (1 2) (2 3 4 5) (1 2 3))) = ((1 2 3
4) (2 3 4 5))

I've written a procedure that compares 2 lists ( list x and list y )
(longerlist x y), if (length x) > (length y) then returns y, vise
versa
if (length x) = (length y) returns both x and y.

Example :
(longerlist '(1 2 3) '(4 5 6 7)) = (4 5 6 7)
(longerlist '(1 2 3 4) '(5 6 7 8)) = ((1 2 3 4) (5 6 7 8))

I can't really think of any other ways to do the final goal other than
using (longerlist x y)
but I am stuck on how to actually use (longerlist) in (longest)
can someone point me to the right direction?

If (longerlist) shouldnt be used in (longest) then any other ways to
approach this?

Thank you for helping!

Theres many ways to do this. Personally I would iterate over the
list, checking the length of each element, and then build up a new list
containing the longest elements.

(let ((ret '()))
   (for-each (lambda (a)
              (cond ...))
            origlist)
   (reverse! ret))

Note that if x contains 2 elements, and y contains 256 million, it's
not useful to compute the length of y (scanning 256 million cdr that
is fetching from the RAM 1 billion octets!)

(define (longer? x y)
  ; Return: (>= (length x) (length y))
  (cond
    ((null? y) #t)
    ((null? x) #f)
    (t (longer? (cdr x) (cdr y)))))

> Example :
> (longerlist '(1 2 3) '(4 5 6 7)) = (4 5 6 7)
> (longerlist '(1 2 3 4) '(5 6 7 8)) = ((1 2 3 4) (5 6 7 8))

> I can't really think of any other ways to do the final goal other than
> using (longerlist x y)
> but I am stuck on how to actually use (longerlist) in (longest)
> can someone point me to the right direction?

The question is how to come with an algorithm to do something.

How would you do it by hand?

> If (longerlist) shouldnt be used in (longest) then any other ways to
> approach this?

longer? or your longerlist is ok: you must use what you must use.

--
__Pascal Bourguignon__
http://www.informatimago.com
http://pjb.ogamita.org

azn_style_pri@hotmail.com skrev:

> If (longerlist) shouldnt be used in (longest) then any other ways to
> approach this?

An alternative:

   1. Find the length of the longest list
      Hint: Use map and length to generate a list of lengths.

   2. Find the maximum element of the list in 1.
      Hint: Use apply and max.

   3. Use the function filter in combination with
         (lambda (a-list)
            (= (length a-list) max))
      where max is the maximum from 2.

--
Jens Axel Sgaard

Great! I got this thing to work.
Thanks for the help!

I will move on to play with some other functions and procedures =)

By the way, to Jens, the alternative way works really good as well,
thanks for the suggestion!

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc