Standard ML : How to access column from list of list in standard ML

折月煮酒 提交于 2019-12-24 11:47:13

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!