Implementing AlgorithmM of TAOCP vol 4 fascicle 2 using Haskell -


i have tried implement title says because of lazy evaluation cannot take result:

data algorithmm = algorithmm {fm::[int],fa::[int],fj::int,fn::int} m1a :: algorithmm->(io (),algorithmm) m1a (algorithmm m j n) = (return (),algorithmm (2:m) j n)  m1b :: algorithmm->(io (),algorithmm) m1b (algorithmm m j n) = (return (),algorithmm m (take n $! repeat 0) j n)   m2 algo = (visit false $ fa algo,algo)   visit true l =do              mapm (\z->putstr $ show z) l              putstr "\n"     visit false (x:xs) =                           mapm (\z->putstr $ show z) xs                           putstr "\n" initn :: algorithmm->(io (),algorithmm) initn (algorithmm m j n) = (return (),algorithmm m j ((length m)-1))  m3 :: algorithmm->(io (),algorithmm) m3 (algorithmm m j n) = (return (),algorithmm m n n)   m4 :: algorithmm->(io (),algorithmm) m4 (algorithmm m j n) = if (a !! j) == (m !! j) - 1 m4 (algorithmm m (setajzero j) (j-1) n) else  (return (),algorithmm m j n)  setajzero (x:xs) 0 = 0:xs        setajzero (x:xs) j = x:(setajzero xs (j-1))  m5 :: algorithmm->(io (),algorithmm) m5 (algorithmm m j n) = if j==0 (return (),algorithmm m j n) else m2 (algorithmm m j n)  bind :: (io(),algorithmm)->(algorithmm->(io (),algorithmm))->(io(),algorithmm) bind g f = f $! snd g   testalgorithmm = m1a (algorithmm [2,2,2] [] 0 0) `bind` initn `bind` m1b `bind` m2 `bind` m3 `bind` m4 `bind` m5  main =     let (x,y) = testalgorithmm     x 

when run above code interpreter take

exception: prelude.(!!): index large

what think list in m1a not expand n+1 m4 throws exception

any ideas? thanks.

as poster above me said, improperly indexed lists. replace (a !! j) == (m !! j) - 1 (a !! (j - 1)) == (m !! (j - 1)) - 1 , work.

however, of superfluous return () statements not anything. seems have misunderstanding of role of monads , io. seem believe laziness makes code not work; while technically true, problem not haskell lazy rather have not told want compute.

i have modified code eliminate of return () statements. note have not changed execution of code. execute in exact same order original.

data algorithmm = algorithmm {fm::[int],fa::[int],fj::int,fn::int} deriving show  m1a :: algorithmm -> algorithmm m1a (algorithmm m j n) = algorithmm (2:m) j n  m1b :: algorithmm -> algorithmm m1b (algorithmm m j n) = algorithmm m (take n $! repeat 0) j n  m2 :: algorithmm -> (string, algorithmm) m2 algo = (visit false $ fa algo,algo)   visit true l = (concatmap show l) ++ "\n"         visit false (x:xs) = concatmap show xs ++ "\n"  initn :: algorithmm -> algorithmm initn (algorithmm m j n) = algorithmm m j ((length m)-1)  m3 :: algorithmm-> algorithmm m3 (algorithmm m j n) = algorithmm m n n   m4 :: algorithmm -> algorithmm m4 (algorithmm m j n) = if (a !! (j - 1)) == (m !! (j - 1)) - 1 m4 (algorithmm m (setajzero j) (j-1) n) else algorithmm m j n  setajzero (x:xs) 0 = 0:xs        setajzero (x:xs) j = x:(setajzero xs (j-1))  m5 :: algorithmm -> (string, algorithmm) m5 (algorithmm m j n) = if j==0 ("", algorithmm m j n) else m2 (algorithmm m j n)  testalgorithmm = s0     (s0, a) = m2 $ m1b $ initn $ m1a (algorithmm [2,2,2] [] 0 0)           b       = m5 $ m4 $ m3 

if inspect m2 notice call visit false every time. can further reduce

m2 :: algorithmm -> (string, algorithmm) m2 algo = ((\xs -> concatmap show xs ++ "\n") $ tail $  fa algo,algo) 

i have replace visit function branch of function corresponding false.

then there issue of happens in testalgorithmm. again, execution same (i have removed strictness property). note since value of testalgorithmm s0, not evaluate m5 $ m4 $ m3 a because value of not needed produce desired output. original code impossible see happening, when remove of obfuscation glaringly obvious.

creating string in middle of chain of function applications seems way of saving intermediate state. if case, should st monad. same things io except can't print or putstrln. but, can these things after have performed of computation.

if want entire chain of algorithm transformations calculated, must following:

testalgorithmm = (b,s0)     (s0, a) = m2 $ m1b $ initn $ m1a (algorithmm [2,2,2] [] 0 0)           b       = m5 $ m4 $ m3 

this outputs intermediate string value, in case want see it.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -