- inner [1,2,3] [4,5,6]; val it = 32 : int(Assume that the two lists have the same length.)
/ 1 2 3 \ \ 4 5 6 /is
/ 1 4 \ | 2 5 | \ 3 6 /An m-by-n matrix can be represented in SML as a list of m rows, each of which is a list of length n. For example, the first matrix above is represented as the list
[[1,2,3],[4,5,6]]Write an efficient SML function to compute the transpose of an m-by-n matrix:
- transpose [[1,2,3],[4,5,6]]; val it = [[1,4],[2,5],[3,6]] : int list listAssume that all the rows in the matrix have the same length.
Hint: You can make very good use of map here.
/ 0 1 \ / 1 2 3 \ * | 3 2 | = / 9 11 \ \ 4 5 6 / \ 1 2 / \ 21 26 /Write an SML function to do matrix multiplication:
- multiply [[1,2,3],[4,5,6]] [[0,1],[3,2],[1,2]]; val it = [[9,11],[21,26]] : int list listAssume that the dimensions of the matrices are appropriate.
Hint: Use inner, transpose, and map.
- val cplus = curry op+; val cplus = fn : int -> int -> int - val plus3 = cplus 3; val plus3 = fn : int -> int - plus3 10; val it = 13 : intWhat are the types of curry and uncurry?
- powerset []; stdIn:18.1-18.12 Warning: type vars not generalized because of value restriction are instantiated to dummy types (X1,X2,...) val it = [[]] : ?.X1 list list - powerset [1,2,3]; val it = [[],[3],[2],[2,3],[1],[1,3],[1,2],[1,2,3]] : int list listNote that powerset [] is not a syntactic value, and hence is not allowed to be polymorphic under SML97's value restriction. Also note that it doesn't matter what order the elements of powerset [1,2,3] come out in.
- fun twice f = fn x => f (f x); val twice = fn : ('a -> 'a) -> 'a -> 'aor, using SML function composition operator o, by
- fun twice f = f o f; val twice = fn : ('a -> 'a) -> 'a -> 'aIf we also define
- fun succ n = n+1; val succ = fn : int -> intthen we can form expressions like
twice (twice (twice (twice succ)))Less obviously, we can form expressions like
twice twice twice twice succin which (by ML's conventions) the function applications are associated to the left. Figure out rules that give the values of these expressions, for any number of occurrences of twice. (Approach this problem experimentally.)