This is a pretty cool technique! Here's a work program that inspired you (I really didn’t read the article, except to look at the picture, so this may not be exactly what the author intended, but it works):
looper :: (inputT -> feedfwdT -> feedbackT -> (feedbackT, outputT)) -> inputT -> feedfwdT -> outputT looper f input primer = output where (feedback, output) = f input primer feedback min_feedback :: (Ord a) => [a] -> Maybe a -> a -> (a, [a]) min_feedback [] (Just p) _ = (p, []) min_feedback (x:xs) partial_min minimum = (feedback, minimum:output) where new_partial_min = case partial_min of Nothing -> Just x Just p -> Just $ min xp (feedback, output) = min_feedback xs new_partial_min minimum min_looped :: (Ord a) => [a] -> [a] min_looped input = looper min_feedback input Nothing main = print $ min_looped [1,4,6,2,6,3,-1,6,3,6,10]
The key point here is that you need more than a feedback channel, you also need a direct channel to determine the minimum value on the first pass through the loop. My ASCII art skills are not up to the standard set in the article, so you just need to do this with this picture:
A look-ahead value is the minimum value that is still displayed in the list. The primer launches a direct connection channel. The feedback channel returns the result value from the direct access channel up to the beginning of the list. Finally, the feedback value becomes the minimum value that is used to populate the output list.
source share