Question

Use scheme to solve this:

MUST USE A COMBINATION OF MAP AND FILTER or MAP AND APPLY:

Map/Apply/Filter 7. Perfect Squares (10 Points) Using a combination of map, apply and/or filter write a function named perfect-squares that takes exactly one argument, a list of numbers. This function should return only the numbers that are perfect squares, which we will define as having square roots which are whole numbers. You may not use recursion to solve this problem, and must use some combination of the three functions mentioned above, although not all need to be used Hint: To test if something is a whole number, use the built in Scheme function integer? Example output is shown below > (perfect-squares (123456789)) (1 4 9 > (perfect-squares (15 16 17 24 25 26 25)) 16 25 25) > (perfect-squares (2 3 5 6))

THIS ANSWER IS NOT WHAT I NEED:

(define perfect-square-helper
(lambda (x y)
(if (null? x)
(reverse y)
(if (integer? (sqrt (car x)))
(perfect-square-helper (cdr x) (cons (car x) y))
(perfect-square-helper (cdr x) y)))))

(define perfect-square
(lambda (x)
(perfect-square-helper x '())))


(define x '(1 2 9 16 5 64))

(perfect-square x)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

7.

(define perfect-square-helper
(lambda (x y)
(if (null? x)
(reverse y)
(if (integer? (sqrt (car x)))
(perfect-square-helper (cdr x) (cons (car x) y))
(perfect-square-helper (cdr x) y)))))

(define perfect-square
(lambda (x)
(perfect-square-helper x '())))


(define x '(1 2 9 16 5 64))

(perfect-square x)

//OUTPUT and code screen shot

1 (define perfect-square-helper (lambda (xy) (if (null? x) 4 5 (reverse y) (perfect-square-helper (cdr x) (cons (car x) y)) (

=> (1 9 16 64)

this is the perfect -squares function that can be used on the example output

Add a comment
Know the answer?
Add Answer to:
Use scheme to solve this: MUST USE A COMBINATION OF MAP AND FILTER or MAP AND...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that...

    Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that it is tail-recursive. You may use the letrec form but do not use any additional forms and functions besides those we've talked about in class. (define filter (lambda (input-list func)     (cond ((null? input-list) '())           ((func (car input-list))            (cons (car input-list) (filter (cdr input-list) func)))           (else            (filter (cdr input-list) func))))) 2. Test your filter function on '(25 -22 44 56...

  • Map, Filter, and Reduce are three functions commonly used in functional programming. For this assignment you...

    Map, Filter, and Reduce are three functions commonly used in functional programming. For this assignment you will be implementing all three, along with three minor functions that can be passed to them. Map The map function takes as arguments a function pointer and a integer vector pointer. It applies the function to every element in the vector, storing the results in-order in a new vector. It then returns a pointer to a new vector. You should pass your square function...

  • Function 4: my-map In CLISP define your own function that duplicates the functionality of mapcar from...

    Function 4: my-map In CLISP define your own function that duplicates the functionality of mapcar from the standard library. You may not use the built-in mapcar function as a helper function. Your implementation must be recursive. ·         Input: The input to my-map is a function that takes a single argument and a homogeneous list of elements of the same data type compatible with the procedure. Note: the function argument can be named or anonymous (lambda). ·         Output: A new list...

  • USE THE PYTHON ONLY Sudoku Game Check Please write a Python program to check a Sudoku...

    USE THE PYTHON ONLY Sudoku Game Check Please write a Python program to check a Sudoku game and show its result in detail. This is an application program of using 2-dimensional arrays or lists. Each array or list is a game board of 9 x 9. Your program must check the 9 x 9 game board, and report all the problems among 9 rows, 9 columns, and 9 squares. As you can see, you must pre-load the following four games...

  • I could not figure how to do it and tried times and times and still could...

    I could not figure how to do it and tried times and times and still could finish under gcc. so please help in c language.thanks Write a function isqrt that computes the integer square root of a nonnegative inte- ger. Write a unit test that adequately exercises your function. The function prototype should be int isqrt (int n, int isr) Your function should return 0 if successful and -1 otherwise. Lack of success means that n is negative or s...

  • I need this written in C++. Magic Squares. An n x n matrix that is filled...

    I need this written in C++. Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. The following algorithm will construct magic n x n squares; it only works if n is odd: Place 1 in the middle of the bottom row. After k has been placed in the (i,j) square,...

  • Part 3: More Numeric Analysis Now add three more functions to the num_stats module you created...

    Part 3: More Numeric Analysis Now add three more functions to the num_stats module you created in part 2. These will determine if a given positive integer is EVEN, PERFECT and ABUNDANT. Each of these functions should accept a single integer as an argument and return whether the integer meets the criteria for that classification. Here's some IPO notation to get you started: # function: is_even # input: a positive integer # processing: determines if the supplied number is even...

  • C++ Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is...

    C++ Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. The following algorithm will construct magic n x n squares; it only works if n is odd: Place 1 in the middle of the bottom row. After k has been placed in the (i,j) square, place k+1 into the square...

  • The purpose of this lab is to practice working with Python's higher order functions and ternary...

    The purpose of this lab is to practice working with Python's higher order functions and ternary operator. Write all of your code in one file named lab2.py. You can write the code for items 1 through 6 directly inside the main function. Let a be the list of values produced by list(range(1, 11)). [1 point] Using the map function and a lambda argument, write an expression that produces a list of the cubes of the values in a. Assign the...

  • Question4 please (1). Let f: Z → Z be given by f(x) = x2. Find F-1(D)...

    Question4 please (1). Let f: Z → Z be given by f(x) = x2. Find F-1(D) where (a) D = {2,4,6,8, 10, 12, 14, 16}. (b) D={-9, -4,0, 16, 25}. (c) D is the set of prime numbers. (d) D = {2k|k Ew} (So D is the set of non-negative integer powers of 2). (2). Suppose that A and B are sets, C is a proper subset of A and F: A + B is a 1-1 function. Show that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT