Combining a column of lists in OCaml

前端 未结 2 902
梦如初夏
梦如初夏 2021-01-23 20:18

I want to essentially transpose a matrix in OCaml (without using recursion or any sort of looping)

For example, if I have the following matrix: [[1;2];[3;4]]

2条回答
  •  走了就别回头了
    2021-01-23 20:23

    Assuming your list of lists is rectangular, this Standard ML code translates to OCaml as such:

    let rec transpose xss =
        match xss with
            | [] -> []
            | []::_ -> []
            | _ -> List.map List.hd xss :: transpose (List.map List.tl xss)
    

    It extracts the first column (List.map List.hd xss) and recursively combines it with the extraction of the remaining columns, after having removed the already extracted column (List.map List.tl xss).

    The explicit recursion that still remains in this function cannot easily be replaced by mapping / folding, since those would address one row at a time, where the recursion scheme above addresses (a part of) all rows at once. You might have more luck with unfolding / anamorphism:

    let rec unfold f a =
        match f a with
        | Some (b, a') -> b :: unfold f a'
        | None -> []
    
    val unfold : ('a -> ('b * 'a) option) -> 'a -> 'b list = 
    

    where 'a could be the gradual reduction of your input row matrix, and 'b the matrix columns:

    let transpose =
        unfold (function
                | [] -> None
                | []::_ -> None
                | m -> Some (List.map List.hd m, List.map List.tl m))
    

提交回复
热议问题