map (\xs -> zip xs [1..]) list
とりあえず入力。
Prelude List> map (\xs -> zip xs [1..]) list:1:26: Not in scope: `list'
listなんて作ってないもんな・・・
Prelude List> map (\xs -> zip xs [1..]) [1,2]:1:27: No instance for (Num [a]) arising from the literal `1' at :1:27 Probable fix: add an instance declaration for (Num [a]) In the list element: 1 In the second argument of `map', namely `[1, 2]' In the definition of `it': it = map (\ xs -> zip xs ([1 .. ])) [1, 2]
???
まず、\xs -> zip xs [1..]は、引数xsをとる関数。で、zip xs [1..]を評価してその結果を返す。
zipは・・・
http://www-users.cs.york.ac.uk/~ndm/hoogle/generated/Prelude.f.zip.htm
makes a list of tuples, each tuple conteining elements of both lists occuring at the same position In: zip [1,2,3] [9,8,7]
Out: [(1,9),(2,8),(3,7)]
tupleをつくる、と。
ということはzip xs [1..]はxsと[1..]からtupleを作る。
で、mapは
http://www-users.cs.york.ac.uk/~ndm/hoogle/generated/Prelude.f.map.htm
Prelude.map :: (a -> b) -> [a] -> [b]
returns a list constructed by appling a function (the first argument) to all items in a list passed as the second argument
map f [ ] = []
map f (x:xs) = f x : map f xs
In: map abs [-1,-3,4,-12]
Out: [1,3,4,12]
型aから型bをつくる関数(a->b)が引数その1、型aのリストが引数その2で型bのリストが得られる、と・・・
この場合は、関数(a->b)がzip xs [1..]だから、リストを引数にとる。
map (\xs -> zip xs [1..]) [1,2]だと、数字の1と2が適用されるからエラーになる。
Prelude List> map (\xs -> zip xs [1..]) [ [1,2] ] [ [(1,1),(2,2)] ] Prelude List> map (\xs -> zip xs [1..]) [ [1,2,3],[4,5,6] ] [ [(1,1),(2,2),(3,3)],[(4,1),(5,2),(6,3)] ]
できたー!