程序代写代做 C Haskell More List Operations

More List Operations
Exercise 1
WriteafunctionunMaybethattakesalistofMaybe a,andreturnsalistcontainingtheelements.
Any Nothing’s present in the original list should be discarded. You will have to work out the type declaration. Make it as general as possible.
> unMaybe [Just 1, Nothing, Just 2]
[1,2]
> unMaybe [Nothing, Just ”hello”, Just ”world”] [”hello”,”world”]
Exercise 4
If one list has more elements than the other, just add the rest of the non-empty list to the end. >riffle [1,2,3] [4,5,6]
[1,4,2,5,3,6)
>riffle [1,2,3) [18,28,30,48,50,68)
(1,10,2,30,3,30,40,50,60)
>riffle [‘h’,’s’, ‘e’,’l’, ‘l’] [‘a’,’k’]
“haskell” Exercise 6
repeatApply takes 3 inputs:
• A fJnction f, with the same input and output type.
• Anintegern,indicatingthenumberoftimestoapplythefunction. • An element x with suitable type to insert into the function.
Output is the result of applying the function f to x, n many times. (So if n=3, the output should be f(f(f(x))). Applying f zero many times just returns x unchanged.) Make the type as general as can be.
Write a function that takes two lists and performs a ‘riffle” shuffle, alternating back and forth to return all elements from both lists.
I Write the repeatApply function. >repeatApply (*2) 3 1
8>repeatApply ( ++ ” NO “) 5 “OH” “OH NO NO NO NO NO”

Implement positiveSum, average, magnitude, dot using higher order functions.
positiveSum :: [Integer] -> Integer
Computes the sum of only the positive numbers in the list, ignoring negative numbers.
>positiveSum [1,-2,3] 4
average :: [Double] -> Double
Computes the average of a list of Double. The average of the collection {x1, x2 ,••• , xn } is defined to
be x1+zz+…+z..-J
magnitude : : [Double] -> Double
Computes the magnitude of a vector, represented as a [Double). Magnitude of a vector is defined as
X1
x� + x� +… + x� (Hint: You will need the sqrt function, which computes square dot :: [Double] -> [Double] -> Double
Computes the dot product of two vectors, represented by [Double]. The dot product is defined as
X1 Yt
X2 Y2
Yn
=✓
Xn
roots.)

Problem 35
Problem 15
(“”) Replicate tile elements of a list a given number of times. Example:
• (repli • (ab c) 3) (A A A 8 B B C C C)
Example in Haskell: A> repli “abc” 3
•aaabbbccc”
Problem 19
(0) Rotate a list N places to the left.
Hint: Use the predefined functions length and(++). Examples:
• (rotate ‘(a b c def g h) 3) (DEF G HA BC)
• (rotate ‘(ab c def g h) -2) (G �ABC DE f)
Examples in Haskell:
A > r o t a t e [ • a1 1 b 1 • c 1 , 1 d ‘ , • e • , ‘ f 1 , • g 1 , • h • ] 3
IA �.s�.::ir – JWnu3Al=;J
.A ‘ DINGDIAN •• EDUCATION
,,
A> rotate [‘a”,’b’,’c’,’d’,’e’,’f’,’g’,’h’] (-2) “ghabcdef”
SolutiOns
Problem 31
(u) Detennine whether a given integer number is prime. Example:
� (is-prune 7) T
Example in Haskell: A> isPrioe 7
True
Solutions
“defghabc”
(“”) Determine the prime factors of a given positive integer. Construct a flat list containing the prime factors in ascending order. Example:
• (prime-factors 31S) (3 3 5 7)
Example in Haskell: A> prirneFactors 315
[3, 3, s, 7] Solutions