functional programming - Haskell Time Issue -
it second question haskell , thought algorhythm not bad , gives faster results in c++ , python , there problem @ haskell cant give me 10^25 (maybe gives dont wait much) , question ask me value ,please guide me fix guy now.
##euler 169## giveres 0 _= 1 giveres 1 _= 1 giveres x = if search x send x else let res = if rem 2 == 0 (giveres (quot 2) x) + (giveres (quot 2-1) x) else giveres (quot 2) x in snd (head ((a,res):x)) search _ [] = false search (x:xs) = if == fst x true else search xs send (x:xs) = if == fst x snd x else send xs
code long because remembering system shortening time in haskell not efficient
f 0 _ = 1 f = if rem 2 == 0 giveres (quot 2) + giveres (quot 2-1) else giveres (quot 2)
is first form of code
- for goodness sakes, clean code people can read it. example, rewrite
snd (head ((a,res):x)) --> res
. suggestion: add type signatures. - extract common subexpressions (
quot
) named let bindings reduce work. - memoize calls giveres. give largest benefit. if search
[haskell] memo
should lots of results. - don't use list set test membership (
search
) followed partial function lookup (send
). instead consider usingmap
orhashmap
containers or unordered-containers libraries.
Comments
Post a Comment