Transpose of an nxm matrix using SML and Pattern Matching

Scheme

Public Domain

This code takes an input matrix as a list of lists, and outputs the transpose of the matrix as a list of lists

Download (right click, save as, rename as appropriate)

Embed

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
fun getHeads [[],[],[]] = []
| getHeads [] = []
| getHeads alist = [hd (hd alist)] @ getHeads (tl alist);

fun removeHeads [[],[],[]] = []
| removeHeads [] = []
| removeHeads blist = [tl (hd blist)] @ removeHeads (tl blist);

fun transp [[],[],[]] = []
| transp [] = []
| transp clist = [getHeads clist] @ transp (removeHeads clist);