need help.. 
Author Message
 need help..

I need to define a function (everythird)  which take a list x, it must produce
a list of the 1st, 4th, 7th..etc items, following by the 2nd. 5th, 8th etc
items followed by the third, 6th, 9th etc items

EX: (everythird '(1 2 3 4 5 6 7)      => (1 4 7 2 5 3 6)

in my implementation i need to make (everythird)  loops 3 times..to get the
right result....ANY IDEA???

;;;(list-ref) return the nth elements of the list items
(define (list-ref items n)
  (if (= n 0)
        ( car items)
        (list-ref (cdr items) (- n 1))))

;;;(evr-3) return every third element starting from the nth element of the list
l
(define (evr-3 n l)
     (if (> (+ n 1) (length l))
         '()
         (cons (list-ref l n) (evr-3 (+ n 3) l))))

(define (everythird l)
  (cond ((null? l)'())
        ((< (length l) 4) '())
        (else (append ((lambda (n l) (evr-3 n l)) 0 l) (everythird (cdr l))))))

(everythird '(1 2 3 4 5 6 7))    ;;;=>(1 4 7 2 5 3 6 4 7)   !!!NOT GOOD (4 and
7 shouldn't be there!!!!!!!!



Fri, 12 Apr 2002 03:00:00 GMT  
 need help..

Quote:

>(define (everythird l)
>  (cond ((null? l)'())
>    ((< (length l) 4) '())
>    (else (append ((lambda (n l) (evr-3 n l)) 0 l) (everythird (cdr l))))))

>(everythird '(1 2 3 4 5 6 7))    ;;;=>(1 4 7 2 5 3 6 4 7)   !!!NOT GOOD (4 and
>7 shouldn't be there!!!!!!!!

The problem is that you keep recursing even after you pass the third
element.  But you don't even need recursion, since there are a fixed number
of calls defined by the problem.  It's simply:

(define (everythird l)
  (append (evr-3 0 l) (evr-3 1 l) (evr-3 2 l)))

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



Fri, 12 Apr 2002 03:00:00 GMT  
 need help..

Quote:

> EX: (everythird '(1 2 3 4 5 6 7)      => (1 4 7 2 5 3 6)

> in my implementation i need to make (everythird)  loops 3 times..to
> get the right result....ANY IDEA???

You may have it loop once, building 3 lists simultaneously (7 4 1) (5 2)
and (6 3), and finally build your result using some kind of
reverse-append (this final step corresponds in fact to a second
traversal of the list). You thus get a tail-recursive implementation.

--

| VERIMAG - Centre Equation - 2, ave. de Vignate - 38610 Gieres - France
| Tel : (33 | 0) 476 63 48 46 -- Fax : (33 | 0) 476 63 48 50



Mon, 15 Apr 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Need help launching external non rexx command using VXREXX (VIO support needed) in OS/2

2. Java in VRML code...need help...or need a piano/keyborad.wrl

3. Need help to grok c extension that needs to use callbacks with data

4. HELP - I need help

5. HELP!! Need help with browse window

6. Help Files, Need Help

7. help i need help

8. HELP! NON-PROGRAMMER NEEDS HELP

9. HELP: Need help with Ms-Dos batch lang..

10. I need help........programming help!

11. Need Help How Can Help me

12. Need Help How can Help ?

 

 
Powered by phpBB® Forum Software