Could someone please answer numbers 3 through 6. I need these to be programmed i
ID: 3664768 • Letter: C
Question
Could someone please answer numbers 3 through 6. I need these to be programmed in Haskell l. Compute the nth Fibonacci number when Fi = F2 = 1 and Fn = Fn-1 + Fn-2 2. Write a function to reverse a string without using the built-in reverse method. 3 Wrte a function hat counts the occurrence of a specifiedcr i strng 4. Remove the leading spaces from a string without using trim or strip 5. Remove the trailing spaces from a string without using trim or strip 6. Remove both the leading and trailing spaces from a string without using trim or strip 4 Remove the leading spaces from a sring witini opExplanation / Answer
1.
-- fibonacci numbers
-- returns the nth fibonacci number
fib :: (Num b, Num a, Eq a) => a -> b
fib n = fib2 0 1 n where
fib2 a b 0 = a
fib2 a b n = fib2 b (a+b) (n-1)
2
-------------------------------------------------------------------------------
myReverse :: [a] -> [a]
myReverse [] = []
myReverse stringToReverse = myReverse' stringToReverse []
where
myReverse' [] stringToReturn = stringToReturn
myReverse' (x:xs) stringToReturn = myReverse' xs (x:stringToReturn)
-- myReverse' (x:xs) stringToReturn = myReverse' xs ([x]++stringToReturn)
3.
-- Count the number of times a character appears in a string.
--
count :: Char -> String -> Int
count c [] = 0
count c (x:xs) | c == x = 1 + count c xs
| otherwise = count c xs
4, 5 and 6
-- Remove leading and trailing whitespace from a string
-- Given the string " hello " return the string "hello".
unwords (words " hello ")