Tuesday, June 2, 2020
Programming Haskell Functions Essay - 1650 Words
Programming Haskell Functions (Other (Not Listed) Sample) Content: Programming Haskell FunctionsÃâName of student:Name of institution: 1 Evaluate the following expressions. For any expression that cannot be evaluated, write ERROR, and give reasons for the error. * 6 + 2 * 4Answer:14 * fst (2,4) + snd (3,6)Answer:8 * [4, 5, 6] ++ []Answer:[4,5,6] * (reverse . length) "ghci"Answer:ERROR. Correct expression is (reverse . length) "ghci" * length ["ghci", "Haskell"] + 1Answer:3 * even 7.23Answer:ERROR 7.23 is not integer. If question was even 7 it would return False as 7 is odd number * map reverse [[5,1],[1,2,4,0]]Answer:[[1,5],[0,4,2,1]] * ((^2) . length) [[5],[1]]Answer:4 2 For each of the following built-in functions and operators, you are required to state what it does and write down its type. In each case you will receive full marks if you give the most general type. For example, the function Id returns its input unchanged and has the type a - a. * (*)Multiplication.:: Num a = a - a - aType number * Length:: [a] - IntReturns leng th of data in integer type * Odd:: Integral a = a - Boolreturns boolean true if the number is odd. False otherwise * Reverse:: [a] - [a]reverses what is inside []. Example reverse [5,6,7] will output [7,6,5] * (++)concatenate two strings 3 Each of the functions below has a definition that results in a compile-time error. In each case explain why there is an error and provide an appropriate definition for the function. * sumPair :: (Int,Int) - IntsumPair n = head n + snd nWhen corrected this is an example application:ghci: sumPair (4,7)11Ans:Couldn't match expected type `[Int]' with actual type `(Int, Int)'Correct function definitionsumPair :: (Int, Int) - IntsumPair (x,y) = x + ymain = print (sumPair (4,7)) * beginWithSameLetter :: String - String - BoolbeginWithSameLetter s1 s2= if head s1 == head s2then "Same"else "Not the same"When corrected this is an example application:ghci: beginWithSameLetter "Dan" "Russell""Not the same"Ans:replace bool with String in function definition as bool can return only true or false. "Same" and "not same" are the return values which are of type StringbeginWithSameLetter :: String - String - StringbeginWithSameLetter s1 s2= if head s1 == head s2then "Same"else "not same"main = print (beginWithSameLetter "Dan" "Russell") * hasEvenLength :: String - BoolhasEvenLength w = (length . even) wWhen corrected this is an example application:ghci: hasEvenLength "ghci"TrueAns:hasEvenLength :: String - BoolhasEvenLength w = (even.length) wmain = print (hasEvenLength "ghci") 4 Each of the following expressions are meant to evaluate to the value written below it. In each case correct the expression without changing the data so that this is the case. For example, snd (3,5) * 2 6 The expression evaluates to 10. It should have been written as fst (3,5) * 2. * (reverse . head) ["one","two","three"]"eerht"Ans: (reverse.last) ["one","two","three"] * sum (map (+1) [1..5])10 * filter (0) (filter even [3,0,-1,2,-3,6])[-1,-3]Ans: filter (0) (filter odd [3,0,-1,2,-3,6]) * length (show 24) + 763Ans: div 76 24 * (map toUpper) (head ["Dan", "Barry"])["dan"]Ans: (map toLower) (head ["Dan", "Barry"]) 5 (a) Explain the problem with each of the following function names: * Module"module" cannot be used for a function name as it is a keyword for defining module. A module keyword is used to define a collection of siliar * "double""double" cannot be used for a function name as it is a keyword for data type double * 3plus3plus cannot be used for function name as a function name cannot start with number * SUMSUM cannot be used for function name as that is the name for a built in function(b) Define functions that match each of the following function type specifications. The purpose of each function is indicated by its name and illustrated by an example. 1 squareThenAddOne :: Num a = a - aghci: squareThenAddOne 526Ans:squareThenAddOne n = n*n + 1 2 bothOdd :: Int - Int - Boolghci: bothOdd 15 5TrueAns: bothOdd x y = odd x odd y 3 makeAPair :: Int - Char - (Char,Int)ghci: makeAPair 5 'C'('C', 5)Ans: 4 convertToDecimalText :: Int - Int - Stringghci: convertToDecimalText 5 3"5.3"Ans:convertToDecimalText :: Int - Int - Stringghci: convertToDecimalText 5 3"5.3" * i. Describe the similarities and differences of the Char and String types. Your answer should include a discussion of the functions and operators that may be applied to the types.Ans:char data type c... Programming Haskell Functions Essay - 1650 Words Programming Haskell Functions (Other (Not Listed) Sample) Content: Programming Haskell FunctionsÃâName of student:Name of institution: 1 Evaluate the following expressions. For any expression that cannot be evaluated, write ERROR, and give reasons for the error. * 6 + 2 * 4Answer:14 * fst (2,4) + snd (3,6)Answer:8 * [4, 5, 6] ++ []Answer:[4,5,6] * (reverse . length) "ghci"Answer:ERROR. Correct expression is (reverse . length) "ghci" * length ["ghci", "Haskell"] + 1Answer:3 * even 7.23Answer:ERROR 7.23 is not integer. If question was even 7 it would return False as 7 is odd number * map reverse [[5,1],[1,2,4,0]]Answer:[[1,5],[0,4,2,1]] * ((^2) . length) [[5],[1]]Answer:4 2 For each of the following built-in functions and operators, you are required to state what it does and write down its type. In each case you will receive full marks if you give the most general type. For example, the function Id returns its input unchanged and has the type a - a. * (*)Multiplication.:: Num a = a - a - aType number * Length:: [a] - IntReturns leng th of data in integer type * Odd:: Integral a = a - Boolreturns boolean true if the number is odd. False otherwise * Reverse:: [a] - [a]reverses what is inside []. Example reverse [5,6,7] will output [7,6,5] * (++)concatenate two strings 3 Each of the functions below has a definition that results in a compile-time error. In each case explain why there is an error and provide an appropriate definition for the function. * sumPair :: (Int,Int) - IntsumPair n = head n + snd nWhen corrected this is an example application:ghci: sumPair (4,7)11Ans:Couldn't match expected type `[Int]' with actual type `(Int, Int)'Correct function definitionsumPair :: (Int, Int) - IntsumPair (x,y) = x + ymain = print (sumPair (4,7)) * beginWithSameLetter :: String - String - BoolbeginWithSameLetter s1 s2= if head s1 == head s2then "Same"else "Not the same"When corrected this is an example application:ghci: beginWithSameLetter "Dan" "Russell""Not the same"Ans:replace bool with String in function definition as bool can return only true or false. "Same" and "not same" are the return values which are of type StringbeginWithSameLetter :: String - String - StringbeginWithSameLetter s1 s2= if head s1 == head s2then "Same"else "not same"main = print (beginWithSameLetter "Dan" "Russell") * hasEvenLength :: String - BoolhasEvenLength w = (length . even) wWhen corrected this is an example application:ghci: hasEvenLength "ghci"TrueAns:hasEvenLength :: String - BoolhasEvenLength w = (even.length) wmain = print (hasEvenLength "ghci") 4 Each of the following expressions are meant to evaluate to the value written below it. In each case correct the expression without changing the data so that this is the case. For example, snd (3,5) * 2 6 The expression evaluates to 10. It should have been written as fst (3,5) * 2. * (reverse . head) ["one","two","three"]"eerht"Ans: (reverse.last) ["one","two","three"] * sum (map (+1) [1..5])10 * filter (0) (filter even [3,0,-1,2,-3,6])[-1,-3]Ans: filter (0) (filter odd [3,0,-1,2,-3,6]) * length (show 24) + 763Ans: div 76 24 * (map toUpper) (head ["Dan", "Barry"])["dan"]Ans: (map toLower) (head ["Dan", "Barry"]) 5 (a) Explain the problem with each of the following function names: * Module"module" cannot be used for a function name as it is a keyword for defining module. A module keyword is used to define a collection of siliar * "double""double" cannot be used for a function name as it is a keyword for data type double * 3plus3plus cannot be used for function name as a function name cannot start with number * SUMSUM cannot be used for function name as that is the name for a built in function(b) Define functions that match each of the following function type specifications. The purpose of each function is indicated by its name and illustrated by an example. 1 squareThenAddOne :: Num a = a - aghci: squareThenAddOne 526Ans:squareThenAddOne n = n*n + 1 2 bothOdd :: Int - Int - Boolghci: bothOdd 15 5TrueAns: bothOdd x y = odd x odd y 3 makeAPair :: Int - Char - (Char,Int)ghci: makeAPair 5 'C'('C', 5)Ans: 4 convertToDecimalText :: Int - Int - Stringghci: convertToDecimalText 5 3"5.3"Ans:convertToDecimalText :: Int - Int - Stringghci: convertToDecimalText 5 3"5.3" * i. Describe the similarities and differences of the Char and String types. Your answer should include a discussion of the functions and operators that may be applied to the types.Ans:char data type c...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.