【发布时间】:2026-01-25 16:15:02
【问题描述】:
我正在做一个学校任务,我得到了一些示例代码,以后可以使用。我理解这段代码的 90%,但是有一个小行/函数我一生都无法弄清楚它的作用(顺便说一句,我对 Haskell 很陌生)。
示例代码:
data Profile = Profile {matrix::[[(Char,Int)]], moleType::SeqType, nrOfSeqs::Int, nm::String} deriving (Show)
nucleotides = "ACGT"
aminoacids = sort "ARNDCEQGHILKMFPSTWYVX"
makeProfileMatrix :: [MolSeq] -> [[(Char, Int)]]
makeProfileMatrix [] = error "Empty sequence list"
makeProfileMatrix sl = res
where
t = seqType (head sl)
defaults =
if (t == DNA) then
zip nucleotides (replicate (length nucleotides) 0) -- Row 1
else
zip aminoacids (replicate (length aminoacids) 0) -- Row 2
strs = map seqSequence sl -- Row 3
tmp1 = map (map (\x -> ((head x), (length x))) . group . sort)
(transpose strs) -- Row 4
equalFst a b = (fst a) == (fst b)
res = map sort (map (\l -> unionBy equalFst l defaults) tmp1)
{-Row 1: 'replicate' creates a list of zeros that is equal to the length of the 'nucleotides' string.
This list is then 'zipped' (combines each element in each list into pairs/tuples) with the nucleotides-}
{-Row 2: 'replicate' creates a list of zeros that is equal to the length of the 'aminoacids' string.
This list is then 'zipped' (combines each element in each list into pairs/tuples) with the aminoacids-}
{-Row 3: The function 'seqSequence' is applied to each element in the 'sl' list and then returns a new altered list.
In other words 'strs' becomes a list that contains the all the sequences in 'sl' (sl contains MolSeq objects, not strings)-}
{-Row 4: (transpose strs) creates a list that has each 'column' of sequences as a element (the first element is made up of each first element in each sequence etc.).
--}
我已经为代码中每个标记的行写了一个解释(我认为到目前为止是正确的)但是当我试图弄清楚第 4 行的作用时我被卡住了。我理解“转置”位,但我根本无法弄清楚内部映射函数的作用。据我所知,“地图”函数需要一个列表作为第二个参数才能运行,但内部地图函数只有一个匿名函数,但没有可操作的列表。非常清楚,我不明白整个内部行 map (\x -> ((head x), (length x))) . group . sort 做了什么。请帮忙!
奖金!:
这是另一段我想不通的示例代码(从未使用过 Haskell 中的类):
class Evol object where
name :: object -> String
distance :: object -> object -> Double
distanceMatrix :: [object] -> [(String, String, Double)]
addRow :: [object] -> Int -> [(String, String, Double)]
distanceMatrix [] = []
distanceMatrix object =
addRow object 0 ++ distanceMatrix (tail object)
addRow object num -- Adds row to distance matrix
| num < length object = (name a, name b, distance a b) : addRow object (num + 1)
| otherwise = []
where
a = head object
b = object !! num
-- Determines the name and distance of an instance of "Evol" if the instance is a "MolSeq".
instance Evol MolSeq where
name = seqName
distance = seqDistance
-- Determines the name and distance of an instance of "Evol" if the instance is a "Profile".
instance Evol Profile where
name = profileName
distance = profileDistance
尤其是这部分:
addRow object num -- Adds row to distance matrix
| num < length object = (name a, name b, distance a b) : addRow object (num + 1)
| otherwise = []
where
a = head object
b = object !! num
如果你不想解释这个,你不必解释我只是对“addRow”实际上试图做什么有点困惑(详细)。
谢谢!
【问题讨论】:
-
很酷,您对 Haskell 感兴趣,但最好将“奖金”部分提取到一个单独的问题中,因为这两个问题彼此无关。
标签: list class dictionary haskell anonymous-function