2012-05-25 3 views
2

행렬을 펼치는 데 문제가 있습니다. 다음은 programm의 출력이 어떻게 보일 것인가입니다. 나는 조금 붙어있다.하스켈의 행렬 연산

unfoldMatrix :: [ [a] ] -> [a] 
Main> unfoldMatrix [[1, 2, 3], 
       [4, 5, 6], 
       [7, 8, 9], 
       [10, 11, 12]] 
[1,4,7,10,11,12,9,6,3,2,5,8] 

내 코드가 작동하지만 출력의이 형식에

[[1,4,7], [8,9], [6,3], [2], [5] []]

원하는대로 작동하도록 코드를 변경하는 방법은 무엇입니까?

transpose2:: [[a]]->[[a]] 
transpose2 ([]:_) = [] 
transpose2 x = (map head x) : transpose2 (map tail x) 


unfoldMatrix:: [[a]]->[[a]] 
unfoldMatrix ([]:_) = [] 
unfoldMatrix x =(map head x):unfoldMatrix(tail2(x)) 

rotate90 :: [ [ a ] ] -> [ [ a ] ] 
rotate90 = (map reverse).transpose2 

tail2:: [[a]]->[[a]] 
tail2 = (tail).rotate90 
+6

흠, 당신은 [목록으로 목록의 목록을 변환 ...] 함수 (찾고있는 http://www.haskell.org/hoogle/?hoogle = % 5B + % 5Ba % 5D + % 5D + - % 3E + % 5Ba % 5D) – rampion

+0

thanks :) 유용한 링크! –

+1

'unfoldMatrix x = (map head x) : unfoldMatrix (tail2 (x))'에서':'를'++'로 간단히 relace 할 수 있습니다. –

답변

0

당신은 모든 하위 목록이 동일하게 긴 경우, 그 Data.List에서 transpose과 동일합니다, transpose2 필요하지 않습니다. 그래서 펼쳐 단순히 것

Prelude Data.List> let unfold [email protected]((_:_):_) = map head xxs ++ unfold (map reverse . transpose $ map tail xxs); unfold _ = [] 
Prelude Data.List> unfold [[1,2,3],[4,5,6],[7,8,9]] 
[1,4,7,8,9,6,3,2,5] 
Prelude Data.List> unfold [[1,2,3],[4,5,6],[7,8,9],[10,11,12]] 
[1,4,7,10,11,12,9,6,3,2,5,8]