Transpose a matrix in racket (list of lists

前端 未结 3 509
既然无缘
既然无缘 2020-12-10 18:26

I got a list of lists in racket and have to transpose them.

(: transpose ((list-of(list-of %a)) -> (list-of (list-of %a))))

(check-expect (transpose (lis         


        
3条回答
  •  遥遥无期
    2020-12-10 18:59

    for/list can be used sequentially to create a list of lists with transposed items:

    (define (transpose_ lol)                    ; lol is list of lists
      (for/list ((i (length (list-ref lol 0)))) ; loop for length of first inner list
        (for/list ((il lol))                    ; for each inner list (il)
          (list-ref il i))))                    ; get its item
    

    Testing:

    (transpose_ (list (list 1 2 3)
                      (list 4 5 6)))
    

    Output:

    '((1 4) (2 5) (3 6))
    

提交回复
热议问题