Here Are 3 Problems About Racket Programming Language Question 1 Define And Test

Here are 3 problems about Racket Programming Language:

Question #1:

Define and test a procedure memv that takes an element and a list and returns the first cdr whose car is eqv? to the element, or #f if the element is absent from the list.

>  (memv ‘a ‘(a b c))

‘(a b c)

>  (memv ‘b ‘(a ? c))

#f

> (memv ‘b ‘(a b c b))

‘(b c b)

Question #2:

The cartesian-product is defined over a list of sets (again simply lists that by our agreed upon convention don’t have duplicates). The result is a list of tuples (i.e. lists). Each tuple has in the first position an element of the first set, in the second position an element of the second set, etc. The output list should contains all such combinations. The exact order of your tuples may differ; this is acceptable.

>  (cartesian-product ‘((5 4) (3 2 1)))

((5 3) (5 2) (5 1) (4 3) (4 2) (4 1))

Question #3:

The procedure powerset takes a list and returns the power set of the elements in the list. The exact order of your lists may differ; this is acceptable.

>  (powerset ‘(3 2 1))

‘((3 2 1) (3 2) (3 1) (3) (2 1) (2) (1) ())

>  (powerset ‘())

‘(( ))

#lang racket(define (product l1 l2)(foldl append ‘()(map (λ (l3)(map (λ (x) (cons l3 x)) l2))l1)))(define (cartesian-product list)(product (car list) (car(cdr list))))

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *