Use DrRacket, based on #lang racket function; I need a complete answer which can
ID: 3865006 • Letter: U
Question
Use DrRacket, based on #lang racket function; I need a complete answer which can be run
Q:Define a recursive function flatten in Scheme which accepts only a s-list (i.e., a list possibly nested to an arbitrary depth) as an argument and returns it flattened as a list of atoms.
Explanation / Answer
In scheme we can define flatten and check if it is null return () else check the pair for '(' and print the list and remove extra braces. > (define (flatten slist) (cond ((null? slist) '()) ((not (pair? slist)) (list slist)) (else (append (flatten (car slist)) (flatten (cdr slist)))))) Example: (flatten '((10) 12 ((13 34) 51) ((())) (((16))) 7 38 ())) (10 12 13 34 51 16 7 38)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.