[SOLVED] CS 431 Programming Language Concepts Homework 6

30.00 $

Programming resource
Digital learning resource
Category:
✓ Practical programming resource
✓ Suitable for guided study and reference
✓ Tutor guidance available when needed

Description

5/5 - (2 votes)

Homework 6
1. Write a function reduce: (‘a * ‘a -> ‘a) -> a’ list -> ‘a list that
behaves like foldl except that it takes the rst element of the list as the
initial value.
For example, reduce (op -) [1,2,3] evaluates to 3 – (2 – 1) = 2.
This method does not apply to empty list.
For the rest of the questions, use zip, map, and reduce to solve
the problems, where zip function is from homework 4.
The following questions are about vectors and matrix. We rep-
resent row vectors using lists. For example, [2,3,5,4] represents
a row vector of four integers. We represent a matrix using a list
of lists. For example, the matrix

1 2 3
4 5 6

is written as [ [1,2,3], [4, 5, 6] ].
2. Write a function vectorAdd: int list * int list -> int list that
add two integer vectors of the same size.
For example, vectorAdd ([1,2,3], [4,5,6]) should return [5, 7, 9].
3. Write a function svProduct: int * int list -> int list that multi-
ple an integer with an integer list.
For example, svProduct(2, [1,2,3]) should return [2,4,6].
4. Write a function vmProduct that multiple a row vector of size n with a
matrix with n rows and m columns to produce a vector of size m. For
example, vmProduct([1,2,3], [[1,1], [2,1], [3,1]]) should return
[14, 6]. Or,
[1 2 3] 
2
4
1 1
2 1
3 1
3
5 = 1  [1 1] + 2  [2 1] + 3  [3 1]
= [1 1] + [4 2] + [9 3]
= [14 6]
This function uses the functions svProduct and vectorAdd de ned earlier.
1
5. Write a function matrixProduct that multiple a mn matrix with a nk
matrix to obtain a m  k matrix. For example

1 2 3
1 1 1


2
4
1 1
2 1
3 1
3
5 =

v1
v2

=

14 6
6 3

where
v1 = [1 2 3] 
2
4
1 1
2 1
3 1
3
5 =

14 6

and
v2 = [1 1 1] 
2
4
1 1
2 1
3 1
3
5 =

6 3

That is,
matrixProduct([ [1, 2, 3], [1, 1, 1] ], [ [1, 1], [2, 1], [3, 1] ])
= [ [14, 6], [6, 3] ]
This problem will use the function vmProduct de ned previously.
2

Resource details

Understand the Task Before You Use the Resource

Review the requirements, identify the programming concepts involved, study the implementation and test your understanding with your own examples and modifications.