Question

In: Computer Science

In racket Assume that the elements of a list are indexed starting with 1. Write a...

In racket

Assume that the elements of a list are indexed starting with 1. Write a function alts that takes a list of integers xs and returns a pair of lists, the first of which has all the odd-indexed elements (in the same relative order as in xs) and the second of which has all the even-indexed elements (in the same relative order as in xs).

Output should be

(alts (list 7 5 4 6 9 2 8 3)) '((7 4 9 8)5 6 2 3)

(alts (list 5 4 6 9 2 8 3)) '((5 6 2 3) 4 9 8)

Any help would be appreciated, we're not allowed to use cond

Solutions

Expert Solution

Code:

(define (alts List)
(if (null? List) ;;if list is empty returns NULL list
'()
(append (list (first List)) (second List)))) ;;else append odd indexed elements list wit elements of even indexed

;;SUB_Function returns odd indexed List
(define (first List)
(if (null? List) '() ;;if list is empty returns NULL list
(if (null? (cdr List)) List ;;if list has only one element return that element
(cons (car List) ;;else create list with first element and
(first (cdr (cdr List))))))) ;;call function recursively with list starting with next odd index

;;SUB_Function returns Even indexed elements List

(define (second List)
(if (null? List) '() ;;if list is empty returns NULL list
(if (null? (cdr List)) '() ;;if list has no second element return empty list
(cons (car (cdr List)) ;;else create list with second element and
(second (cdr (cdr List))))))) ;;call function recursively with list starting with next even index

Snapshot of Code and Output:


Related Solutions

Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list. (please do on racket language)
Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list.
RACKET a) Write a recursive function (gen-list start end). This function will generate a list of...
RACKET a) Write a recursive function (gen-list start end). This function will generate a list of consecutive integers, from start to end. If start > end then an empty list is generated. For example: (gen-list 1 5) ---> (1 2 3 4 5) b) write a recursive function pair-sum? that takes an integer sequence as generated by the gen-list function in exercise 4 above. This function tests whether any two adjacent values in the given list sum to the given...
Write in Racket Language Write a recursive Racket function "all-same" that takes a string as a...
Write in Racket Language Write a recursive Racket function "all-same" that takes a string as a parameter and evaluates to true iff every character in the string is the same. Note: A string of length 0 or 1 should also evaluate to true.
List and explain the measuring elements (stages) of a dial indicator starting with the primary sensing...
List and explain the measuring elements (stages) of a dial indicator starting with the primary sensing element
1.Write a loop to print a list of numbers starting at 64 and ending at 339....
1.Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax. 2. Write a switch statement that uses the colour of a swab sample of COVID testing vehicle to send a message to a local doctor. Use the messages given for each colour in the table below. Justify your answer. Colour Message Blue “No virus” Yellow “Needs to be under observation” Red “Needs to be admitted in COVID ward” 3.
Write in Racket language. Write a non-recursive Racket function called "keep-short-norec" that takes an integer and...
Write in Racket language. Write a non-recursive Racket function called "keep-short-norec" that takes an integer and a list of strings as parameters and evaluates to a list of strings. The resulting list should be all strings on the original list, maintaining their relative order, whose string length is less than the integer parameter. For example, (keep-short-rec 3 '('abc' 'ab' 'a')) should evaluate to '('ab''a') because these are the only strings shorter than 3.
PLEASE GIVE THE CODE IN RACKET PROGRAMMING RECURSIVE LANGUAGE ONLY Write a Racket function "combine" that...
PLEASE GIVE THE CODE IN RACKET PROGRAMMING RECURSIVE LANGUAGE ONLY Write a Racket function "combine" that takes two functions, f and g, as parameters and evaluates to a new function. Both f and g will be functions that take one parameter and evaluate to some result. The returned function should be the composition of the two functions with f applied first and g applied to f's result. For example (combine add1 sub1) should evaluate to a function equivalent to (define...
Write a loop to print a list of numbers starting at 64 and ending at 339....
Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax.
Write a loop to print a list of numbers starting at 64 and ending at 339....
Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT