-RACKET LANGUAGE ONLY-
Write a non-recursive Racket function "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.
Your solution must not be recursive. You will want to pass a lambda function to "filter" to solve this problem. Note: (string-length s) evaluates to the length of string s.
A few notes on how to use filter:
filter function takes an anonymous(lambda function) and a list as argument. The filter automatically applies the lambda function to each element of the list and retains only those items which satistfy the condition given in the lambda function [for which lambda returns true].
The program:
(define (keep-short-norec x y)
(filter (lambda (i) (< (string-length i) x)) y))
(write (keep-short-norec 3 '("abc" "ab" "a")))
Output:
("ab" "a")
Also, the filter function maintains the relative order of the items in the list.
Get Answers For Free
Most questions answered within 1 hours.