0

署名付きの関数が必要です :: [(Int,Char)] -> [String] 各 Char を取り、それらを対応するペアの整数値に従って文字列のリストに結合します...

すなわち。

[(0,'A'),(1,'B'),(2,'C'),(3,'D'),(0,'E'),(1,'F'),(2,'G'),(3,'H'),(0,'I'),(1,'J'),(2,'K'),(3,'L'),(0,'M'),(1,'N'),(2,'O'),(3,'P')]

リストを生成する必要があります: ["MIEA","NJFB","OKGC","PLHD"]

私はもう試した:

toList :: [(Int,Char)] -> Int -> [String]
toList [] a = []
toList (x:xs) a = [snd(x) | n <-[0..(a-1)], fst(x) == n]:toList xs a

しかし、それは私に与えます:

["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P"]

どんな提案でも大歓迎です。

4

2 に答える 2

3

入力をインデックスでソートしてから、すべてのペアを同じインデックスでグループ化する必要があります。次のようなもの:

map (reverse . map snd) $ groupBy ((==) `on` fst) $ sortBy (compare `on` fst) $ input

$オペレーターなし:

import Data.List
import Data.Function

toList :: [(Int, Char)] -> [String]
toList input = map (reverse . map snd) grouped
  where
  sorted = sortBy (compare `on` fst) input    -- sorted by the index
  grouped = groupBy ((==) `on` fst) sorted    -- grouped by the index
于 2013-02-03T21:10:35.743 に答える
1

課題の期日が過ぎたので、質問全体を含めることができます...関数は、シミュレートされた「画像」を時計回りに 90 度回転するように求められました。私が思いついた解決策 (これが私たちの最初の課題だったので単純な Haskell コードを使用) は次のとおりです。文字列の単純な「正方形」リストでは機能しますが、より長方形のリストでは機能しないようです...理由はまだわかりません。

{- The function rotate90 rotates a "picture" (List of Strings) through
   90 degrees clockwise and prints the list to the standard output device
   using the printPicture function defined in the Thompson Picture.hs file.
-}

-- the following String list is a sample used to test implementation:
pic :: [String]
pic = [ "ABCD", "EFGH", "IJKL", "MNOP", "QRST" ]

rotate90 :: [String] -> IO ()
rotate90 aPic   = printPicture(convertToPic (listPairs aPic (maxOfList(widthOfLines aPic))     ((maxOfList(widthOfLines aPic))*(length(aPic)))) ([0..((maxOfList(widthOfLines aPic))-1)]))

-- takes a picture, the width and the number of Chars in a rectangle
-- bounding the picture (area) and converts it to a list of pairs with
-- an address and Char as per the cypher algorithm:
-- address = floor of (area -1)/ width
listPairs :: [String] -> Int -> Int -> [(Int,Char)]
listPairs [] w area = []
listPairs aPic w area   = [ (((n-1)`mod`w),(aPic!!(floor(fromIntegral(n-1)/(fromIntegral(w))))!!((n-1)`mod`w))) | n <- [1..area] ]

-- takes a list of tuples (pairs) and an Int which is the width of the input picture
-- it retunrs a list of Strings that correspond to the groupings of the pairs.
convertToPic :: [(Int,Char)] -> [Int] -> [String]
convertToPic [] (y:ys)  = []
convertToPic somePair []    = []
convertToPic somePair (y:ys)= reverse([ snd(x) | x <- somePair, ((fst(x)) == y)]):(convertToPic somePair ys)

-- takes a list of Int and returns the maximum value from the list as an Int
maxOfList :: [Int] -> Int
maxOfList []        = 0
maxOfList (x:xs)        = max x (maxOfList xs)

-- takes a list of type String and returns a list of Int of the length of each string
widthOfLines :: [String] -> [Int]
widthOfLines []     = []
widthOfLines (x:xs)     = (length x):(widthOfLines xs)

-- definition from Thompson (textbook) picture.hs file
printPicture :: [String] -> IO ()
printPicture = putStr . concat . map (++"\n")
于 2013-02-20T07:33:03.487 に答える