Je fais une tâche scolaire où on me donne un petit échantillon de code que je peux utiliser plus tard. Je comprends 90% de ce code mais il y a une petite ligne/fonction que pour la vie de moi je n'arrive pas à comprendre ce que ça fait (je suis très nouveau chez Haskell btw).Haskell - J'ai de la difficulté à comprendre un petit bout de code
Exemple de code:
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.).
--}
J'ai écrit une explication pour chaque ligne marquée dans le code (que je pense à ce jour est correct) mais je suis bloqué lorsque je tente de comprendre ce qui fait la ligne 4. Je comprends le bit 'transposer' mais je n'arrive pas à comprendre ce que fait la fonction carte interne. Pour autant que je sache, une fonction 'map' a besoin d'une liste en tant que second paramètre pour fonctionner, mais la fonction map interne n'a qu'une fonction anonyme mais pas de liste à utiliser. Pour être parfaitement clair, je ne comprends pas ce que fait toute la ligne intérieure map (\x -> ((head x), (length x))) . group . sort
. S'il vous plaît aider!
Bonus !:
Voici un autre morceau de code exemple que je ne peux pas comprendre (jamais travaillé avec des classes en 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
Surtout cette partie:
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
Vous n'avez pas besoin d'expliquer celui-ci si vous ne voulez pas que je sois un peu confus quant à ce que 'addRow' essaie réellement de faire (en détail).
Merci!
Il est cool, que vous Je m'intéresse à Haskell, mais il vaudrait mieux extraire la partie «bonus» en une question distincte, parce que ces deux questions ne sont pas liées les unes aux autres. –