Question
I have spent close to 10 hours trying to figure this out and I literally just give up at this point. Please help! FYI this
I have spent close to 10 hours trying to figure this out and I literally just give up at this point. Please help! FYI this is in DrRacket
3. diginlist - takes a list and returns the diggedin version of the list. diggingin a list
( this is not a recognized function for list ) means surrounding the rightmost and leftmost elements in
parenthesis, and repeating the process inwards until the entire list is diggedin. For example:
(diginlist '(4 5 3 2 8)) would return (4 (5 (3) 2) 8).
You can assume that the input list is "flat", meaning that it doesn't contain any lists. If the input
list has an odd number of elements, the center element should be in a list by itself, as shown in
the example above. If the input list has an even number of elements, then there should be an
empty list in the middle. For example:
(diginlist '(4 5 2 8) would return (4 (5 () 2) 8)).
Here is as far as I got:
(define (diginlistHelper N L NL) (cond ((= N 0) '() ((= N 1) (cons (list (car L)) NL)) (else (cons (cons (car L) (list (lastItem L))) NL) (remove 1 L) ) ) ) )
(define (diginlist L) (define length (listLength L)) (define L2 (diginlistHelper length L '() )) (define L3 (diginlistHelper (- length 2) L L2 )) L3 )
(define (lastItem L) (if (null? (cdr L))(car L) (lastItem (cdr L))) )
(define (remove N L) (cond ((eq? N 0) (cdr L)) (else (cons (car L) (remove (- N 1)(cdr L))))) )
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started