haskellfoldfoldable

foldMap Conjoined


I'm trying create this function in Haskell with a given prototype:

The function "foldMap Conjoined" over a list of functions returning Bool values
    should produce a function over the same input type that returns True if all of
    the functions in the list return True for the given input, and False if at
    least one of the functions returns False for the given input. (If the list is
    empty, then all of the zero functions in the list return True.)> 

newtype Conjoined a = Conjoined { getConjoined :: a -> Bool }
       deriving Generic
    
instance Monoid (Conjoined a) where
       f <> g = Conjoined (\x -> True)

The line f <> g is the start of my portion of the code. With that line, the function passes the associativity test, but it doesn't produce the correct result


Solution

  • You can make an new function that returns True if both the first and second function return True, by using (&&). Then the mempty is a [Join] function that is True for all input: source: stackoverflow.com/q/62982832/67579

    taking the same logic and applying it to this 'conjoined' monoid

       "instance Monoid (Conjoined a) where
            Conjoined f <> Conjoined g = Conjoined (\x -> f x && g x)
            mempty = Conjoined (const True)"