-- replace_and_length takes two inputs, a list of strings and a -- single string. It both replaces occurrences of "J END" in the -- input list with the input string, and counts the number of items -- in the list. The function returns a two-tuple containing the -- length and the updated list. replace_and_length [] _ = (0, []) replace_and_length (i:insts) jmp = let (len, rest) = replace_and_length insts jmp in if (i == "J END") then (len+1, jmp:rest) else (len+1, i:rest) -- The helper above can do both of the things we need: Replace, and -- find the length. We just need to call it in such a way that we -- Use one of its outputs (the length) to build one of its inputs -- (the updated jump instruction). patch_jumps lst = new_lst where (len, new_lst) = replace_and_length lst ("J " ++ show(len)) prog = ["LOAD", "CMP", "J END", "STORE", "LOAD", "J END", "STORE"] {- Problem #2: ========== Hypothesis: sum (xs ++ ys) = sum xs + sum ys Base Case: --------- sum ([] ++ ys) = sum [] + sum ys left-hand side: -------------- sum ys ++.1 right-hand side: --------------- sum ys sum.1 Inductive Step: -------------- sum ((x:xs) ++ ys) = sum (x:xs) + sum ys left-hand side: -------------- sum x:(xs ++ ys) ++.2 x + sum(xs ++ ys) sum.2 x + sum xs + sum ys hyp right-hand side: --------------- x + sum xs + sum ys sum.2 Problem #3a: =========== Hypothesis: xs ++ [] = xs Base Case: --------- [] ++ [] = [] [] = [] by ++.1 Inductive Step: -------------- (x:xs) ++ [] = (x:xs) (x : (xs ++ [])) = (x : xs) ++.1 (x : xs) = (x : xs) hyp Problem #3b: =========== Hypothesis: (xs ++ lst2) ++ lst3 = xs ++ (lst2 ++ lst3) Base Case: --------- ([] ++ lst2) ++ lst3 = [] ++ (lst2 ++ lst3) left-hand side: -------------- ([] ++ lst2) ++ lst3 lst2 ++ lst3 ++.1 right-hand side: --------------- [] ++ (lst2 ++ lst3) lst2 ++ lst3 ++.1 Inductive Step: -------------- ((x:xs) ++ lst2) ++ lst3 = (x:xs) ++ (lst2 ++ lst3) left-hand side: -------------- ((x:xs) ++ lst2) ++ lst3 (x:(xs ++ lst2)) ++ lst3 ++.2 x : ((xs ++ lst2) ++ lst3) ++.2 x : (xs ++ (lst2 ++ lst3)) hyp right-hand side: --------------- (x:xs) ++ (lst2 ++ lst3) x : (xs ++ (lst2 ++ lst3)) ++.2 -}