Lucky Triples in Haskell In answering this question on Stack Overflow, I decided to stretch my legs in Haskell a bit to see if I could implement a solution to find the count of lucky triples in a list of ints.

A lucky triple is any triple (j, k, l) where j &lt;= k &lt;= l and l `mod` k == 0 &amp;&amp; k `mod` j == 0. A correct implementation counts how many unique combinations of elements of an [Int] are valid lucky triples.

import Data.List (sort)

answer :: [Int] -&gt; Int
answer xs = foldr ((+) . foldr ((+) . length) 0) 0 $ twoStepFactors
  where
    twoStepFactors = map mapFactors $ mapFactors $ sort xs

mapFactors :: [Int] -&gt; [[Int]]
mapFactors xs = mapFactors' xs []
mapFactors' [] acc = acc
mapFactors' (x:xs) acc = mapFactors' xs newAcc 
  where
    factors = filter ((==0) . (`mod` x)) xs
    newAcc | null factors = acc
           | otherwise = acc ++ [factors]
If mapFactors' is put in a where clause of mapFactors, it isn't globally accessible and you can call it go or something because the scoping already points out it belongs to mapFactors.

Swapping gos argument order lets you say mapFactors = go [].

++ [_] is a smell. Instead of passing down an accumulator and growing it to the right, you can pass it out as the return value and grow it to the left.

mapFactors :: [Int] -&gt; [[Int]]
mapFactors = go where
  go [] = []
  go (x:xs) = newAcc $ go xs where
    factors = filter ((==0) . (`mod` x)) xs
    newAcc | null factors = id
           | otherwise = (:) factors


The filtering can happen after go is done.

mapFactors :: [Int] -&gt; [[Int]]
mapFactors = filter (not . null) . go where
  go [] = []
  go (x:xs) = filter ((==0) . (`mod` x)) xs : go xs


tails helps express go in terms of map:

mapFactors :: [Int] -&gt; [[Int]]
mapFactors = filter (not . null) . map go . tails where
  go [] = []
  go (x:xs) = filter ((==0) . (`mod` x)) xs


Let's use sum from that comment and inline the once-used twoStepFactors (imo if you're only giving a name to explain what something does, use comments)

answer :: [Int] -&gt; Int
answer xs = sum $ map (sum . map length . mapFactors) $ mapFactors $ sort xs


There doesn't seem to be a reason to filter out the []s.

List comprehensions neatly let us skip the empty tail, and get rid of go.

mapFactors :: [Int] -&gt; [[Int]]
mapFactors xss = [filter ((==0) . (`mod` x)) xs | x:xs &lt;- tails xss]