This works :
f :: Int -> Int
f n = gof n where
gof 0 = 1
gof i = i - ms!! ( fs!! (i-1) )
gom 0 = 0
gom i = i - fs!! ( ms!! (i-1) )
fs = [gof j | j <- [0..n]]
ms = [gom j | j <- [0..n]]
m n = gom n where
gof 0 = 1
gof i = i - ms!! ( fs!! (i-1) )
gom 0 = 0
gom i = i - fs!! ( ms!! (i-1) )
fs = [gof j | j <- [0..n]]
ms = [gom j | j <- [0..n]]
However it is really repetitive. Is there a way to avoid just repeating those chunks of code? A few references, this is an adaptation of :
http://jelv.is/blog/Lazy-Dynamic-Programming/
Sequence ref :
https://en.wikipedia.org/wiki/Hofstadter_sequence
I checked it against the numbers :
https://oeis.org/A005378 https://oeis.org/A005379
It generates the right numbers and it is way faster than the basic code which won't go very high at all before it starts having issues with recursion depth.
mandf. This pair of sequences looks like you can do even better though, and generate them corecursively.!!on lists is inefficient, since it has a O(n) cost. If indexing can not be avoided, consider using some O(1) data structure, e.g. arrays. Also recursion depth should not be an issue unless it is really huge -- why do you mention that? (You are not using an ancient Hugs, right?)