I recently started back working throughEoPL and yesterday Isolved exercise 3.15.
Extend the language by adding a new operation
Since I'm using Haskell to complete these exercises this one presented a unique challenge becauseprint
is not a pure function.
To solve it I decided to change the type of my interpreter from
valueOfProgram::Program->Value
to
valueOfProgram::Program->(Value,String)
but this had significant consequences on the readability of my code.
run::String->(Value,String)run=valueOfProgram.parsevalueOfProgram::Program->(Value,String)valueOfProgram(Programexpr)=valueOfExprexprinitEnvwhereinitEnv=Env.extend"i"(NumberVal1)(Env.extend"v"(NumberVal5)(Env.extend"x"(NumberVal10)Env.empty))valueOfExpr::Expr->Environment->(Value,String)valueOfExprexprenv=caseexprofConstn->(NumberValn,"")Varv->(Env.applyenvv,"")Diffab->let(aVal,s)=valueOfExpraenv(bVal,t)=valueOfExprbenvin(NumberVal(toNumberaVal-toNumberbVal),s++t)Zeroe->let(val,s)=valueOfExpreenvin(BoolVal(toNumberval==0),s)Iftestconsequentalternative->let(testVal,s)=valueOfExprtestenvinif(toBooltestVal)thenlet(result,t)=valueOfExprconsequentenvin(result,s++t)elselet(result,t)=valueOfExpralternativeenvin(result,s++t)Letvarebody->let(val,s)=valueOfExpreenv(result,t)=valueOfExprbody(Env.extendvarvalenv)in(result,s++t)Printe->let(val,s)=valueOfExpreenvin(NumberVal1,s++showval++"\n")
See the full changehere.
You see all the drudgery involved to ensure that the output string gets handled correctly.
I was able to use the Writer monad to hide all that drudgery and improve the readability of my code.
Look at it now.
run::String->(Value,String)run=runWriter.valueOfProgram.parsevalueOfProgram::Program->WriterStringValuevalueOfProgram(Programexpr)=valueOfExprexprinitEnvwhereinitEnv=Env.extend"i"(NumberVal1)(Env.extend"v"(NumberVal5)(Env.extend"x"(NumberVal10)Env.empty))valueOfExpr::Expr->Environment->WriterStringValuevalueOfExprexprenv=caseexprofConstn->return$NumberValnVarv->return$Env.applyenvvDiffab->doaVal<-valueOfExpraenvbVal<-valueOfExprbenvreturn$NumberVal(toNumberaVal-toNumberbVal)Zeroe->doval<-valueOfExpreenvreturn$BoolVal(toNumberval==0)Iftestconsequentalternative->dotestVal<-valueOfExprtestenvif(toBooltestVal)thenvalueOfExprconsequentenvelsevalueOfExpralternativeenvLetvarebody->doval<-valueOfExpreenvvalueOfExprbody(Env.extendvarvalenv)Printe->doval<-valueOfExpreenvtell$showval++"\n"return$NumberVal1
See the full changehere.
Takeaways
Write the most obvious Haskell code that solves the problem. Don't worry about what's the best way to do it in Haskell at this point.
Writetests to ensure the code works as expected.
Refactor the code. At this point you have well tested working code but you think you can do better. Now is the best time to learn what techniques or ideas can help you improve the code. Read widely and expand your knowledge of Haskell.
P.S.This was not a post about how to use the Writer monad. It was a post about how the Writer monad helped me to write clearer Haskell code. To learn about the Writer monad I'd recommend the book "Haskell Programming from First Principles".
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse