Splitting list into a list of possible tuples

后端 未结 6 809
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 04:59

I need to split a list into a list of all possible tuples, but I\'m unsure of how to do so.

For example:

pairs [\"cat\",\"dog\",\"mouse\"]

6条回答
  •  感情败类
    2020-11-28 05:48

    Another possibility is to use monadic notation:

    pairs :: (Eq a) => [a] -> [(a,a)]
    pairs l = do
        x <- l
        y <- l
        guard (x /= y)
        return (x, y)
    

    (The most general type for this definition of pairs would be (MonadPlus m, Eq a) => m a -> m (a,a) but I believe there is no instance of MonadPlus other than [] for which it would make sense.)

提交回复
热议问题