How do you write the function 'pairs' in Haskell?
问题 The pairs function needs to do something like this: pairs [1, 2, 3, 4] -> [(1, 2), (2, 3), (3, 4)] 回答1: pairs [] = [] pairs xs = zip xs (tail xs) 回答2: You could go as far as import Control.Applicative (<*>) pairs = zip <*> tail but pairs xs = zip xs (tail xs) is probably clearer. 回答3: Just for completeness, a more "low-level" version using explicit recursion: pairs (x:xs@(y:_)) = (x, y) : pairs xs pairs _ = [] The construct x:xs@(y:_) means "a list with a head x , and a tail xs that has at