2

I want to write something using foldl/foldr or recursion, what performs tasks one after another in the previously performed "state" with the next Job's function. It needs to work with any type of given variable/function. The time is irrelevant now, that's why I used hole instead of it. I can do it with recursion, but for some reason "++" changes it's type, so I get an error.

I tried this one so far:

data Time = Time Int Int
data Job a = Job Time (a -> a) 


perform :: a -> [Job a] -> a
perform s [] = s  
perform s ((Job _ f):xs) = foldl (\x -> f s) s xs
--perform startingpoint [] = s
--perform startingpoint ((Job _ function) : xs) = foldl (\x -> function startingpoint) (function startingpoint) xs

What's the problem? How could I fix this?

  • 1
    `foldl` expects a 2-args function, but you pass `\x -> f s` which only takes one argument. Also, `\x -> f s` never uses the argument `x` -- are you sure you don't need to use it? – chi Dec 06 '21 at 19:57
  • 4
    There's a code smell here (unrelated to your actual problem): you use both pattern matching and a fold. It's almost always right to use one or the other but not both. – Daniel Wagner Dec 06 '21 at 19:58

0 Answers0