代写COMP3400 Functional and Logic Programming Semester One Examinations, 2024调试数据库编程

School of Electrical Engineering & Computer Science

Semester One Examinations, 2024

COMP3400 Functional and Logic Programming

Very Short Answer

Question 1. [20 marks]

The following ten questions are worth two marks each for a total of 20 marks.

Part (a) [2 marks]

For what value of b does fold r (++) b xs == foldl (++) b xs

Part (b) [2 marks]

Let maximum ::  [Integer] -> Integer return the largest integer from a list.  Write a useful quick–check for maximum. We presume xs and ys represent arbitrary non-empty integer lists in the solution.

Part (c) [2 marks]

What type will be inferred for the following function? Be sure to include any necessary class constraints.

foo 0 = 1:2:3:[]

foo n = n :  (foo $ n-1)

Part (d) [2 marks]

Let p :: Integer -> Bool and suppose there are at least ten nonzero x  ::  Integer such that

p x == True. Write a Haskell expression that assigns ten such integers to the list xs ::  [Integer].

Part (e) [2 marks]

Add one set of brackets, round braces (), to the following expression so that it can be executed by the Haskell REPL without producing an error.

pure  (,)  <*>  [1]  <*>  pure  (,)  <*>  [2]  <*>  [3]

Part (f) [2 marks]

Define a function f so that

> : type f

f ::   (a -> b -> [c]) -> (a -> b) -> a -> c

up to renaming the type variables. Your implementation does not have to be total.

Part (g) [2 marks]

Use the Maybe applicative to concatenate Just "Exam-" with Just "Bear" to obtain Just "Exam-Bear".

Part (h) [2 marks]

Give an example of “syntactic sugar” being used in the Haskell language.

Part (i) [2 marks]

Recall the cons operator:  (:)  :: a -> [a] -> [a]. What is the type of  (:) . (:)?

Part (j) [2 marks]

Very briefly explain how input/output was made pure in Haskell.

Short Answer

The following eight questions are worth five marks each for a total of 40 marks.

Question 2. [5 marks]

For each question give the β normal form for the given λ–calculus expression or show the expression is divergent. You must use Haskell ordering.

Part (a) [3 marks]

((λx.xx)(λy.y)) (λz.zz) ab

Part (b) [2 marks]

x ((λy.yy)(λz.zz))

Question 3. [5 marks]

Write a function

runningSum :: Integer -> IO ()

that repeatedly prompts the user (with add:) for numbers until their sum is divisible by seven. The intermediate sums are printed. The first input is the starting sum.

Sample usage . . .

1        > runningSum 10

2        add: 5

3        15

4        add:  -2

5        13

6        add: 1

7        14

8        >

Question 4. [5 marks]

A boolean expression is one that is built from variables, conjunctions (And), disjunctions (Or), and negation (Not). A datatype for representing such expressions is given by

1       data Bexpr = Var Char | And Bexpr Bexpr | Or Bexpr Bexpr |  Not Bexpr

De Morgan’s laws imply

p or q = not ((not p) and (not q))

so it is therefore possible to rewrite every boolean expression without using Or.

Define a function noOr :: Bexpr -> Bexpr that, given a boolean expression, returns an equiv- alent expression that has no Or’s in it.

2       > noOr $ Or (Var‘p’) (Var‘q’)

3       Not (And (Not (Var ‘p’)) (Not (Var ‘q’)))

Do not simplify or evaluate the expression.

Question 5. [5 marks]

We define unzip to be the inverse of the zip operation:

1       unzip [(1,2), (3,4), (5,6)] == ([1,3,5], [2, 4, 6])

Part (a) [4 marks]

Define unzip using a fold (either left or right).  You must include the type–definition.  Your unzip must be equal to a fold and nothing else to receive marks.

Part (b) [1 mark]

What is the type of the function passed to fold in Part (a) of this question?

Question 6. [5 marks]

Vending machines can only dispense coins.  Suppose someone purchases an item (in Aus– tralia) for $1.55 and pays with a $5 note. The machine will dispense back one $2 coin, one $1 coin, zero $0.50 coins, two $.20 coins, zero $0.10 coins, and one $0.05 coin totalling $3.45 in change.

Write a function

change :: Int -> [Int] -> [Int]

that when given a target sum, and list of coin values returns a list of how many coins of each denomination to give back as change while always choosing the most valuable coin first. To avoid oating–point errors we use 200 for $2, 100 for $1 and so on. . .

1        > change 345 [200, 100, 50, 20, 10, 5]

2       [1,1,0,2,0,1]  -- 1*200 + 1*100 + 0*50 + 2*20 + 0*10 + 1*5 = 345

Assume that: target sum is positive and can be made with the available coins; the machine has infinitely many coins of each denomination; the coin values are given in descending value.

Question 7. [5 marks]

The following is a tail recursive function for multiplying nonnegative integers.

1       trMult :: Integer -> Integer -> Integer -> Integer

2        trMult ans x 0 = ans

3        trMult ans x y = trMult (ans+x) x (y-1)

Prove that trMult 0 x y == x * y for all nonnegative integers x and y.

Question 8. [5 marks]

Define a function

mZipWith ::  (a -> b -> c) -> [Maybe a] -> [Maybe b] -> Maybe [c]

which does a zipWith over Maybe types.  Truncate to the length of the shorter list.  Return Nothing if mZipWith tries to “zip” any value with Nothing.

> mZipWith (+) [Just 1, Just 2] [Just 3, Just 4, Just 5]

Just [4, 6]

> mZipWith (+) [Just 1, Just 2] [Just 3, Nothing]

Nothing

> mZipWith (+) [Just 1, Just 2] [Just 3, Just 4, Nothing]

Just [4, 6]

Question 9. [5 marks]

Implement a function makeUnique which removes duplicates from a list.  Note: The ordering of the output does not matter.

> makeUnique [1, 3, 1, 2, 2]

[1,3,2]

> makeUnique "aaabbbccc"

"abc"

Your solution must include a type declaration.

Long Answer

Show your work. Unsupported solutions will receive few or no marks.

Question 10. [10 marks]

Given the type and instance declaration below, prove the second functor law for the Poly type by using induction.

-- Second functor law

fmap (g . h) = fmap g . fmap h

data Poly a = Mono a Integer | Add (Poly a) (Poly a)

instance Functor Poly where

fmap g (Mono c k)          = Mono (g c) k

fmap g (Add polyA polyB) = Add (fmap g polyA) (fmap g polyB)



热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图