4 kyu

Fix it

547 of 552gans

Description:

The fix function can be defined as:

fix :: (a -> a) -> a
fix f = let x = f x in x
fix :: forall a b. ((a -> b) -> a -> b) -> a -> b
fix g v = g (fix g) v

If we regard this as a language primitive, any recursive function can be written without using recursion. Write foldr' and reverse' as non-recursive functions that can be 'fixed' to foldr and reverse as follows:

foldr = fix foldr'
reverse = fix reverse'

For a more detailed explanation of the fix function, see http://en.wikipedia.org/wiki/Fixed-point_combinator

Note: foldr is lazy, so your foldr' should be lazy too. Also, your foldr' need only work on lists - it does not have to work for an arbitrary Foldable functor.

Fundamentals

Stats:

CreatedOct 19, 2014
PublishedOct 19, 2014
Warriors Trained1423
Total Skips332
Total Code Submissions2650
Total Times Completed552
Haskell Completions547
PureScript Completions16
Total Stars89
% of votes with a positive feedback rating93% of 137
Total "Very Satisfied" Votes120
Total "Somewhat Satisfied" Votes14
Total "Not Satisfied" Votes2
Ad
Contributors
  • gans Avatar
  • user578387 Avatar
  • donaldsebleung Avatar
  • JohanWiltink Avatar
  • Voile Avatar
  • Bubbler Avatar
Ad