[Tried to improve assignment, partly based on student feedback. Nils Anders Danielsson **20050908104013] { hunk ./CasesAndRecursionAssignment.ws 14 - p <#>Today you'll work with cases and recursion. + p <#>Today you'll work with cases and recursion. Try to follow the + method for defining functions proposed in the lecture. hunk ./CasesAndRecursionAssignment.ws 18 - follow-up questions, just as before, so you should always read - the answers. + follow-up questions and/or useful information, so you should + always read the answers. hunk ./Rec.hs 28 +even'' :: Integer -> Bool +even'' 0 = True +even'' n | n > 0 = not (even'' (n-1)) +even'' n | n < 0 = not (even'' (n+1)) + +-- Checks if argument is odd. +odd'' :: Integer -> Bool +odd'' n = not (even'' n) + +prop_even'' :: Integer -> Bool +prop_even'' n = even'' (2*n) + +prop_odd'' :: Integer -> Bool +prop_odd'' n = odd'' (2*n + 1) + +-- Checks if argument is even. hunk ./Recursion.ws 8 +divisorUrl = "http://en.wikipedia.org/wiki/Divisor" hunk ./Recursion.ws 38 - , "prop_factorial n = n >= 0 ==> factorial (n+1) `div` factorial n == n+1" + , "prop_factorial n =" + , " n >= 0 ==> factorial (n+1) `div` factorial n == n+1" hunk ./Recursion.ws 49 - , "prop_factorial' n = n >= 0 ==> factorial n `divides` factorial (n+1)" + , "prop_factorial' n =" + , " n >= 0 ==> factorial n `divides` factorial (n+1)" hunk ./Recursion.ws 61 - p <#>The error function can be used to signal invalid - input. It halts the program and prints the given error message. - Try to write understandable error messages. + li $ do + + p <#>The error function can be used to signal invalid + input. It halts the program and prints the given error message. + Try to write understandable error messages. + + fixed $ unlines + [ "Main> factorial (-3)" + , "" + , "Program error: factorial: Negative input not allowed." + ] hunk ./Recursion.ws 73 - fixed $ unlines - [ "Main> factorial (-3)" - , "" - , "Program error: factorial: Negative input not allowed." - ] + li $ p <#>By putting a function name in backquotes we can use it in an + infix style. Writing m `divides` n means + exactly the same thing as writing divides m + n. The reason for doing this is that the + expression becomes easier to read. hunk ./Recursion.ws 133 - p <#>Two ways of solving the problem: + p <#>Three ways of solving the problem: hunk ./Recursion.ws 136 + + li $ fixed $ unlines + [ "-- Checks if argument is even." + , "even :: Integer -> Bool" + , "even 0 = True" + , "even n | n > 0 = not (even (n-1))" + , "even n | n < 0 = not (even (n+1))" + , "" + , "-- Checks if argument is odd." + , "odd :: Integer -> Bool" + , "odd n = not (even n)" + ] hunk ./Recursion.ws 178 - <#>Compute the number of positive divisors of an arbitrary positive + p <#>Compute the number of positive >divisors of an arbitrary positive hunk ./Recursion.ws 181 - arguments. ?> do + integer arguments m and n: + + ul $ do + + li $ p <#>n: We want to know how many divisors n + has in the range 1 to m. + + li $ p <#>m: The number next in turn. We should check + whether it divides n or not. + + <#>If you get stuck, don't hesitate to take a peak at the + solution. ?> do hunk ./Recursion.ws 227 - functions. The names introduced using where are only - visible in the current function definition. We could also - have written divisors as follows: + functions. The names introduced using where are + only visible in the current function definition. We could + also have written divisors as follows (as + proposed in the hint): }