What constitutes a fold for types other than list?

A Fold for Every Occasion We can actually come up with a generic notion of folding which can apply to a whole bunch of different types. That is, we can systematically define a fold function for lists, trees and more. This generic notion of fold corresponds to the catamorphisms @pelotom mentioned in his comment. Recursive … Read more

haskell running out of memory with finite lists

I think you’ve seen elsewhere that your specific problem (creating ordered lists of integers that sum to a given number) is better solved using an alternative algorithm, rather than filtering a huge list of lists of integers. However, getting back to your original issue, it is possible to construct an equivalent of: replicateM p [1..n] … Read more

Euler 43 – is there a monad to help write this list comprehension?

Sure. newtype UniqueSel a = UniqueSel {runUS :: [Int] -> [([Int], a)]} instance Monad UniqueSel where return a = UniqueSel (\ choices -> [(choices, a)]) m >>= k = UniqueSel (\ choices -> concatMap (\ (choices’, a) -> runUS (k a) choices’) (runUS m choices)) instance MonadPlus UniqueSel where mzero = UniqueSel $ \ _ … Read more

Haskell: how to evaluate a String like “1+2”

Your question leaves a lot of room for interpretation. I’m taking a guess you aren’t accustom to building a whole pipeline of lexing, parsing, maybe type checking, and evaluating. The long answer would involve you defining what language you wish to evaluate (Just integers with ‘+’, perhaps all rationals with ‘+’, ‘-‘ ‘*’, “https://stackoverflow.com/”, or … Read more