问题
Suppose I have a list of list list = [[1,2,3],[4,5,6],[7,8,9]] and i want to calculate the sum of the columns. i.e The first column is [1,4,7]and its sum is 12 Second column is [2,5,8] ans sum is 15 and so on
Is there any efficient way(with less complexity) in standard ML to do this?? Please help
回答1:
For example:
fun transpose [] = []
| transpose ([]::xss) = []
| transpose xss = map hd xss :: transpose (map tl xss)
val sum = foldl op+ 0
val sumsOfColumns = map sum o transpose
Example use:
sumsOfColumns [[1,2,3],[4,5,6],[7,8,9]] (* => [12, 15, 18] *)
;)
来源:https://stackoverflow.com/questions/28342394/standard-ml-how-to-access-column-from-list-of-list-in-standard-ml