Sign In
Not register? Register Now!
You are here: HomeOther (Not Listed)Technology
Pages:
6 pages/≈1650 words
Sources:
Level:
APA
Subject:
Technology
Type:
Other (Not Listed)
Language:
English (U.S.)
Document:
MS Word
Date:
Total cost:
$ 36.45
Topic:

Programming Haskell Functions (Other (Not Listed) Sample)

Instructions:

the assignment is all about Haskell GHCI Function Programming, evaluating different question on Haskell GHCI Function Programming.

source..
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 * 4
Answer:
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"] + 1
Answer:
3
* even 7.23
Answer:
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 -> a
Type number
* Length
:: [a] -> Int
Returns length of data in integer type
* Odd
:: Integral a => a -> Bool
returns 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) -> Int
sumPair n = head n + snd n
When corrected this is an example application:
ghci:> sumPair (4,7)
11
Ans:
Couldn't match expected type `[Int]' with actual type `(Int, Int)'
Correct function definition
sumPair :: (Int, Int) -> Int
sumPair (x,y) = x + y
main = print (sumPair (4,7))
* beginWithSameLetter :: String -> String -> Bool
beginWithSameLetter s1 s2
= if head s1 == head s2
then "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 String
beginWithSameLetter :: String -> String -> String
beginWithSameLetter s1 s2
= if head s1 == head s2
then "Same"
else "not same"
main = print (beginWithSameLetter "Dan" "Russell")
* hasEvenLength :: String -> Bool
hasEvenLength w = (length . even) w
When corrected this is an example application:
ghci:> hasEvenLength "ghci"
True
Ans:
hasEvenLength :: String -> Bool
hasEvenLength w = (even.length) w
main = 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) + 76
3
Ans: 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
* 3plus
3plus cannot be used for function name as a function name cannot start with number
* SUM
SUM 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 -> a
ghci:> squareThenAddOne 5
26
Ans:
squareThenAddOne n = n*n + 1
2 bothOdd :: Int -> Int -> Bool
ghci:> bothOdd 15 5
True
Ans: bothOdd x y = odd x && odd y
3 makeAPair :: Int -> Char -> (Char,Int)
ghci:> makeAPair 5 'C'
('C', 5)
Ans:
4 convertToDecimalText :: Int -> Int -> String
ghci:> convertToDecimalText 5 3
"5.3"
Ans:
convertToDecimalText :: Int -> Int -> String
ghci:> 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...
Get the Whole Paper!
Not exactly what you need?
Do you need a custom essay? Order right now:

Other Topics:

  • Data Curation at Scale: the Data Tamer System
    Description: This was a review of a research paper that i had to write for a masters student. This was a academic assignment that i completed in past...
    1 page/≈550 words| APA | Technology | Other (Not Listed) |
  • U2 Research Project: Windows PowerShell
    Description: Windows PowerShell is a .NET-based framework from Microsoft that is used for automating and configuring management tasks...
    1 page/≈275 words| APA | Technology | Other (Not Listed) |
  • Resolving Intellectual Property Rights
    Description: A complete search shows that there exist a similar trademark but not identical to SafePac...
    3 pages/≈825 words| 5 Sources | APA | Technology | Other (Not Listed) |
Need a Custom Essay Written?
First time 15% Discount!