Question

Needs to be done in DrRacket Write a function dotProduct which takes two lists of numbers...

Needs to be done in DrRacket

Write a function dotProduct which takes two lists of numbers representing vectors, and produces their dot product (or reports their incompatibility).
E.g., (dotProduct '(1 2) '(3 4) ) -> 11
(dotProduct '(1 2 3) '(4 5 6)) -> 32
(dotProduct '(1 2 3) '(4 5)) -> *incompatible*

Homework Answers

Answer #1

We can write a recursive function which accepts two lists and in each recursion it picks the first element of both lists multiply them and return the sum of this product with recursive call on remaining list, base condition will be when both list have no elements and in that case we will return 0.

note - car returns the head(first element) of a given list, example - car '(1 2 3) returns 1.

cdr returns the tail of a given list (remaining list after removing head element). example - cdr '(1 2 3) returns '(2 3).

Racket Code -

#lang racket
(define (dotProduct l1 l2) ; two paremeters as list l1 and list l2
(cond ((not (equal? (length l1) (length l2))) (print "*incompatible*")) ;codititon to check if length of both list is same or not, if not then print incompatible otherwise contnue
(else
(cond ((null? l1) 0) ;base case, when both lists have 0 elements left.
(else
(+ (* (car l1) (car l2)) ;pick first element of both list, multiply them and add to the result of recursive call on remaining list(in next line).
(dotProduct (cdr l1) (cdr l2))))))))

Screenshots -

Know the answer?
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
Similar Questions
Write a function count_div5(nested_list) that takes in a list of lists of integers, and returns a...
Write a function count_div5(nested_list) that takes in a list of lists of integers, and returns a list of integers representing how many integers in each sublist of the original were divisible by 5. Examples: >>> count_div5([[5, 3, 25, 4], [46, 7], [5, 10, 15]]) [2, 0, 3] >>> count_div5([]) [] >>> count_div5([[-20, 10, 2, 4, 5], [], [5], [8, 25, 10], [6]]) [3, 0, 1, 2, 0]
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function...
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function `g` representing an increasing function g(x) 2) a number `gmin`, and returns an integer `nmin` such that nmin>0 is the smallest integer that satisfies g(nmin)>gmin. test: def g(n): return 2*n print(lowest_integer(g, 10)) Output: 6
Please write function in Racket language. Write a recursive Racket function "sum-diff" that takes two lists...
Please write function in Racket language. Write a recursive Racket function "sum-diff" that takes two lists of integers that are the same length and evaluates to an integer. The resulting integer should be the sum of the absolute value of the differences between each pair of integers with the same index in the two lists. For example (sum-diff '(-1+2+3)'(123)) should evaluate to 12 because the absolute value of the differences between (-1 and 1), (-2 and 2) and (-3 and...
Write a function text_filter that takes two lists of characters and checks to see if all...
Write a function text_filter that takes two lists of characters and checks to see if all the characters in the first list are included in the second list AND in the same order, BUT possibly with other characters in between. Done in OCAML For example text_filter ['a';'m';'z'] ['1';'a';'2';'m';'3';'z'] = true text_filter ['a';'m';'z'] ['1';'a';'3';'z'] = false text_filter ['a';'m';'z'] ['1';'z';'2';'m';'3';'a'] = false let rec text_filter (xs:char list) (ys:char list) : bool = *) (* Problem 3b. Rewrite the function above so that...
Write a Python function count_bigger that takes two parameters, a nested list of objects and a...
Write a Python function count_bigger that takes two parameters, a nested list of objects and a threshold number. It returns an integer specifying how many of the objects anywhere in the nested list are numbers that are larger than the threshold. (For our purposes, "numbers" are either integers or floats.) There may be objects in the list other than numbers, in which case you would simply ignore them. Here are a couple of examples of how the function should behave...
Write the recursive version of the function decimal which takes in n, a number, and returns...
Write the recursive version of the function decimal which takes in n, a number, and returns a list representing the decimal representation of the number. def decimal(n): """Return a list representing the decimal representation of a number. >>> decimal(55055) [5, 5, 0, 5, 5] >>> decimal(-136) ['-', 1, 3, 6] """
Write a solution to the subset sum problem using Drracket. Write a procedure (subset-sum n nums)...
Write a solution to the subset sum problem using Drracket. Write a procedure (subset-sum n nums) where n is an integer and nums is a list of numbers that returns a subset of the numbers in the list nums whose values sum to n. If no subset of nums does this, the function should return #f. Ex: (subset-sum 12 '(2 10 5 7 3 8 6)) returns '(2 10) or any other subset of '(2 10 5 7 3 8...
Write the function most_factors(numbers) that returns the integer from the list numbers that has the most...
Write the function most_factors(numbers) that returns the integer from the list numbers that has the most factors (divisors without remainder). For example: >>> most_factors([5,10,16,20,25]) 20 # because 20 has the most factors of any of these numbers # 6 factors, i.e., [1, 2, 4, 5, 10, 20] >>> most_factors([1, 2, 3, 4, 5]) 4 # because 4 has the most factors of any of these numbers # 3 factors, i.e., [1, 2, 4] Hints: For each element in numbers, call...
LISP [3 marks] Write a procedure called triangle that takes three numbers as arguments representing the...
LISP [3 marks] Write a procedure called triangle that takes three numbers as arguments representing the three side-lengths of a triangle. The procedure should classify the triangle and return one of the following strings: "Equilateral" "Isoceles" "Scalene". E.g. (triangle 2 3 4) → "Scalene" E.g. (triangle 2 3 2) → "Isoceles"
Write a Python function to count the number of even numbers from a list of numbers....
Write a Python function to count the number of even numbers from a list of numbers. Write a Python function that takes a list and returns a new list with unique elements of the first list. Sample List : [1,2,3,3,3,3,4,5] Unique List : [1, 2, 3, 4, 5]