module MutualDiag where import ExprInterp import Parser (readExpr) -- Rewrite the following mutual recursive code -- -- let f = \n -> if n == 0 then 0 else 3 * f (n - 1) - g (n - 1) -- g = \n -> if n == 0 then 1 else g (n - 1) + 2 * f (n - 1) -- in f x * g y -- -- into the toy functional language from lecture without recursion, -- by extending the diagonal trick to two mutually recursive functions. -- -- For convenience and actually readable syntax :) just complete the string in -- buildString below (look for TODO). Then buildExpr below will parse it into -- Expr using the provided parser. -- -- Note: you need to write \\ for each \ in a string literal. -- -- Note: When defining mkG, you may not use mkF. The interpreter has actually -- been modified for this (independent bindings). buildString :: Integer -> Integer -> String buildString x y = "let " ++ "mkF = \\TODO -> \\n -> if n==0 then 0 else 3 * TODO (n-1) - TODO (n-1) ;" ++ "mkG = \\TODO -> \\n -> if n==0 then 1 else TODO (n-1) + 2 * TODO (n-1) ;" ++ "in mkF TODO " ++ show x ++ " * " ++ "mkG TODO " ++ show y buildExpr :: Integer -> Integer -> Expr buildExpr x y = readExpr (buildString x y)