{- |
Description: List of Goldbach pairs
Copyright: Copyright (C) 2023 Yoo Chung
License: GPL-3.0-or-later
Maintainer: dev@chungyc.org

Some solutions to "Problems.P41" of Ninety-Nine Haskell "Problems".
-}
module Solutions.P41 (goldbachList) where

import           Problems.P40

-- | Given a range of integers by its lower and upper limit,
-- return a list of all Goldbach compositions for the even numbers in the range.
goldbachList :: Integral a => a -> a -> [(a,a)]
goldbachList :: forall a. Integral a => a -> a -> [(a, a)]
goldbachList a
m a
n = (a -> (a, a)) -> [a] -> [(a, a)]
forall a b. (a -> b) -> [a] -> [b]
map a -> (a, a)
forall a. Integral a => a -> (a, a)
goldbach ([a] -> [(a, a)]) -> [a] -> [(a, a)]
forall a b. (a -> b) -> a -> b
$ (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (a
n >=) [a
m',a
m'a -> a -> a
forall a. Num a => a -> a -> a
+a
2..]
  where m' :: a
m' | a
m a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
2 = a
4
           | a -> Bool
forall a. Integral a => a -> Bool
even a
m = a
m
           | Bool
otherwise = a
ma -> a -> a
forall a. Num a => a -> a -> a
+a
1