haskell - How is this listArray populated? -


having correctly answered euler question 92 using brute force solution read through thread 92 looking other haskell solutions. last solution blisteringly fast has me stumped how implements array showing if numbers 0 567 boil down chains ending in 1 (false) or 89 (true).

import data.list import data.array import data.char  b = (is89!) . sum . map ((^2) . digittoint) . show is89 = listarray (0, 567) $ false:false:(map b [2..88] ++ [true] ++ map b [90..567]) 

i thought understood lazy nature of haskell , way can use prior values in lists generate future values such in generating primes, fibonacci numbers or such like. in instance indexes seem dance (within bounds of 0 567) , lost how value of index ends false or true.

for instance first value in first map 2 when squared , summed 4. index 4 has not been prepopulated yet. values prepopulated far can see 0,1 , 89.

could walk me through how array populated

haskell's laziness calculating every value once, , array rather classical memoization technique.

so asked calculate is89 ! 2, check array , see element has not yet been evaluated, follow definition , see evaluate it, need evaluate is89 ! 4, that. again check array , see isn't evaluated yet. , on, check 16, 37, 58, , arrive @ 89, check array , see value has been evaluated! (per definition true). backtrack , update index 58 "be evaluated" , contain true, , on 37, 16, 4, , 2, , when return , value asked true.

next time, suppose, asked is89 ! 11, check array, , see not evaluated yet, , need is89 ! 2, evaluated 1 earlier! use result of evaluation past.

in end have achieved lookup table populated values demanded, i.e memoization.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -