module Calculator where import Question calculator = do h2 $ text "Using the computer as a calculator" p <#>One use of a computer is as a calculator, i.e. to interactively make computations. In this course, we will use a program named Hugs for this purpose. The program is started from the terminal window by typing hugs: fixed "> hugs" p <#>While reading this text, you should really perform what is being described. If you haven't started Hugs already, do it now! As a response you get the Hugs welcome message: fixed $ unlines [ "__ __ __ __ ____ ___ _________________________________________" , "|| || || || || || ||__ Hugs 98: Based on the Haskell 98 standard" , "||___|| ||__|| ||__|| __|| Copyright (c) 1994-2003" , "||---|| ___|| World Wide Web: http://haskell.org/hugs" , "|| || Report bugs to: hugs-bugs@haskell.org" , "|| || Version: November 2003 _________________________________________" , "" , "Haskell 98 mode: Restart with command line option -98 to enable extensions" , "" , "Type :? for help" , "Prelude> " ] p <#>For the time being, we aren't concerned about what is being said here. Instead we focus on the last line where the cursor (i.e. where the typing appears on the screen) is. On this line we find the text Prelude>, which indicates that Hugs is ready to be given a command. We will come back to the meaning of the word Prelude at a later stage. The simplest command is to just type an expression, after which Hugs will calculate and print out the value of that expression. prompts' [ ("4+5*6", "34") , ("", "") ] p <#>Now, as indicated by the cursor, the system is prepared for another command. We can repeat this interaction an infinite number of times, as demonstrated in the dialogue below: prompts' [ ("4*(8-5)+3", "15") , ("3.5/(3+4)*4.5", "2.25") , ("sin 1.57", "0.999999682931835") ] <#>The first of these three expressions consists of a multiplication, an addition and a subtraction. In which order are they carried out and what values are being multiplied, added and subtracted? ?> do p <#>The expression to be computed is 4*(8-5)+3. p <#>First 8-5 is computed, which results in 3, then 4*3=12 and finally 12+3=15. <#>What result can we expect when we omit all parentheses in the first expression, i.e. 4*8-5+3? ?> do p <#>The expression to be computed is 4*8-5+3. Unlike the previous expression, the right hand side operand of * is simply the number 8. First, 4*8=32 is computed, then 5 is subtracted, resulting in 27 and finally 3 is added, giving the final result 30. Try it out yourselves in Hugs: prompts' [ ("4*8-5+3","30") ] p <#>It is important to understand in what order subcomputations are carried out and why. <#>When computing the second expression, what operations are carried out and in what order? ?> do p <#>The expression to be computed is 3.5/(3+4)*4.5. p <#>First 3+4 is computed, which results in 7, then 3.5/7=0.5 and finally 0.5*4.5=2.25. p <#>The last expression suggests that Hugs is familiar with the trigonometric functions, as well as the exponential function (exp), the square root function (sqrt), and many more. p <#>We aren't always given a value as result when Hugs is given an expression to calculate. There can be something wrong with the expression, making it impossible to calculate a value: prompts' [ ( "3+5*" , "ERROR: Syntax error in expression \ \(unexpected end of input)" ) , ( "div 4 0", "Program error: divide by zero") ] p <#>In the first case, the expression is syntactically wrong since the multiplication has no right hand side operand. In the second case, we're trying to divide by zero, which results in an error during the computation. To recognize different kinds of errors and to understand their causes take a lot of time in the beginning. We will return to this later on.