Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

b. (10 points) Define a recursive function reverse in Scheme which accepts only an S-list s as an argument and reverses the elements of s

image text in transcribed
b. (10 points) Define a recursive function reverse in Scheme which accepts only an S-list s as an argument and reverses the elements of s in linear time (i.e., time directly proportional to the size of s), O(n). You may use only define, lambda, let, cond, null?, cons, car, and cdr in reverse. You may not use append or letrec in your solution. Examples: Welcome to DrRacket, version 7.5 [3m) Language: racket, with debugging, memory limit: 128 MB CPS 352 - Assignment 2 by Your Name > (reverse (1 2 3 4 5)) (5 4 3 2 1) > (reverse (1)) (1) > (reverse (21) ) (1 2) > (reverse (night and day)) (day and night) > (reverse (1 02 (3)) (45)) ) ((4 5) (2 (3)) 1) c. (10 points) Define a recursive function flatten in Scheme which accepts only an s-list (i.e., a list, possibly nested to an arbitrary depth) as an argument and returns it flattened as a list of atoms. Examples: Welcome to DrRacket, version 7.5 (3m). Language: racket, with debugging, memory limit: 128 MB. CPS 352 - Assignment 2 by Your Name > (flatten'()) O > (flatten (O)) () > (flatten '((())) () > (flatten' (a b c d)) (a b c d) > (flatten' (a (b) 0 ( 0) 0 d ((e)))) (a b c d e) > (flatten (((a b)) (c) () d ((le) () (()))))) (a b c d e f) > (flatten (a (b c) (((d)) (e)) (f g(h)))) '(a b c d e f g h) b. (10 points) Define a recursive function reverse in Scheme which accepts only an S-list s as an argument and reverses the elements of s in linear time (i.e., time directly proportional to the size of s), O(n). You may use only define, lambda, let, cond, null?, cons, car, and cdr in reverse. You may not use append or letrec in your solution. Examples: Welcome to DrRacket, version 7.5 [3m) Language: racket, with debugging, memory limit: 128 MB CPS 352 - Assignment 2 by Your Name > (reverse (1 2 3 4 5)) (5 4 3 2 1) > (reverse (1)) (1) > (reverse (21) ) (1 2) > (reverse (night and day)) (day and night) > (reverse (1 02 (3)) (45)) ) ((4 5) (2 (3)) 1) c. (10 points) Define a recursive function flatten in Scheme which accepts only an s-list (i.e., a list, possibly nested to an arbitrary depth) as an argument and returns it flattened as a list of atoms. Examples: Welcome to DrRacket, version 7.5 (3m). Language: racket, with debugging, memory limit: 128 MB. CPS 352 - Assignment 2 by Your Name > (flatten'()) O > (flatten (O)) () > (flatten '((())) () > (flatten' (a b c d)) (a b c d) > (flatten' (a (b) 0 ( 0) 0 d ((e)))) (a b c d e) > (flatten (((a b)) (c) () d ((le) () (()))))) (a b c d e f) > (flatten (a (b c) (((d)) (e)) (f g(h)))) '(a b c d e f g h)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced Database Systems For Integration Of Media And User Environments 98

Authors: Yahiko Kambayashi, Akifumi Makinouchi, Shunsuke Uemura, Katsumi Tanaka, Yoshifumi Masunaga

1st Edition

9810234368, 978-9810234362

More Books

Students also viewed these Databases questions

Question

Should we implement a balanced scorecard?

Answered: 1 week ago