Question: solve using racket Do Not Use... ... any of Racket's primitive higher-order functions in any of your solutions, including map , apply , and filter
solve using racket
Do Not Use...
- ... any of Racket's primitive higher-order functions in any of your solutions, including map, apply, and filter.
- ... any function that converts a list argument to another datatype. Process the list.
- ... a let expression or an internal define in any of your solutions.
Organizing Code
- Put your solutions in the provided questions posted at the bottom.
A.Write a structurally recursive function (list-of? type? lst) that takes two arguments: a boolean predicate type? and list lst. list-of? returns a true if every item in lst is of the type?. For example:
> (list-of? number? '(1 2 3 "Eugene" 4 5)) #f
B.Write a mutually recursive function (nlist-* nlst1 nlst2) that takes two arguments, two n-lists that have the same shape.
::= () | ( . ) ::= |
nlist-* returns an n-list with the same shape as its arguments, where each value is the product of the corresponding values in nlist1 and nlist2. For example:
> (nlist-* '(1 2 3 4 5 6 7 8) '(8 7 6 5 4 3 2 1)) '(8 14 18 20 20 18 14 8) > (nlist-* '((((((1 -2 (3 -4)) 5 ((7 (-8) 9 -10))) 11 -12)) 13 -14)) '((((((4 2 (2 -1)) 6 ((3 ( 2) 5 -10))) 7 -8)) 9 -10))) '((((((4 -4 (6 4)) 30 ((21 (-16) 45 100))) 77 96)) 117 140))
nlist-* should be mutually recursive with the function numberexp-*, which returns the product of two number expressions.
C.Write a mutually recursive function (string-lengths str-lst) that takes one argument, a string-list.
::= () | ( . ) ::= |
string-lengths returns a n-list with the same shape as str-lst, where each string has been replaced by its length. For example:
> (string-lengths '("Write" "a" "mutually" "recursive" "function" ("max-length" "str-list") "that" "takes" "one" "argument" ("a" "string-list"))) '(5 1 8 9 8 (10 8) 4 5 3 8 (1 11)) string-lengths should be mutually recursive with the function string-lengths-se, which processes a a string expression. Recall that Racket provides string-length as a primitive function.
D.Write a mutually recursive function named (n-list? obj) that takes one argument, which can be any Racket value. n-list? returns true if and only if obj is an n-list. For example:
> (n-list? '(2019 2015 2011)) #t > (n-list? '(1 (2 (3 4) 5) 6)) #t > (n-list? '(1 (2 (3 a) 5) 6)) ; oops, there's an 'a #f
n-list? should be mutually recursive with the function num-expr?, which returns true if its argument is a number expression and false if not. Even though the argument to this function can be any Racket object, you can still use the definition of n-list to design your function. An n-list must either be an empty list or a pair whose first is a number-expression and whose rest is an n-list. Similarly for number expressions. So your functions must follow the definition in order to tell if their arguments do!
E.Write a mutually recursive procedure (prefix->infix binary-exp), which takes a binary expression in prefix notation as an argument.
::= ( ) ::= | ::=
prefix->infix returns an equivalent infix expression as its value.
::= ( ) ::= | ::=
For example:
> (prefix->infix '(+ 4 5)) '(4 + 5) > (prefix->infix '(* (+ 4 5) (+ 7 6))) '((4 + 5) * (7 + 6))
prefix->infix should be mutually recursive with the function number-expr->infix, which returns an infix version of a number expression. (This is a great place to use program derivation, if you like.)
This module defines the five functions specified in ;; Homework 5 as an importable module. Notice how each ;; each function has a body and returns a default value. ;; Writing stubs of this sort enables us to load the file ;; and run tests, even if the tests fail. ;; ;; MODIFIED: ;; CHANGE: ;; #lang racket (provide list-of? nlist-* string-lengths n-list? prefix->infix) ;; -------------------------------------------------------------------------- ;; Problem A (structural recursion) ;; -------------------------------------------------------------------------- (define list-of? (lambda (type? lst) #f)) ;; -------------------------------------------------------------------------- ;; Problem B (mutual recursion) ;; -------------------------------------------------------------------------- (define nlist-* (lambda (nlst) '())) ;; -------------------------------------------------------------------------- ;; Problem C (mutual recursion) ;; -------------------------------------------------------------------------- (define string-lengths (lambda (str-lst) '())) ;; -------------------------------------------------------------------------- ;; Problem D (mutual recursion) ;; -------------------------------------------------------------------------- (define n-list? (lambda (obj) #f)) ;; -------------------------------------------------------------------------- ;; Problem E (mutual recursion) ;; -------------------------------------------------------------------------- (define prefix->infix (lambda (binary-exp) ; This is a default value, used as a placeholder. '(1 + 2))) ; It will fail all but one test. ;; --------------------------------------------------------------------------
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
