use haskell haskell haskell haskell to write following : 2pts) Create a function
ID: 3600134 • Letter: U
Question
use haskell haskell haskell haskell to write following :
2pts) Create a function that adds two Ints together. Then create a function, using the first, that adds a number to 10, using partial application.
2pts) Using partial application/currying, create a function that checks if a Char is a lowercase letter
2pts) Write a function that takes another function and an Int and applies it 3 times.
4pts) Using recursion, write a function that takes a function and a list of Ints, and applies the function to every other element in the list (applies to first, skips the second, and so on), and returns a new list with both the transformed and untransformed elements in place
2pts) Write a function like zipWith, except that it takes 3 lists and a function that takes 3 parameters, and returns a list with the function applied to each corresponding element in the 3 lists
2pts) Write a function that takes a function (Int -> Int) and an
Int and returns (Int, Int), where the first element is the Int
without the function applied to it, and the second is the result of
applying the function to the Int. That is, if we call the function toTuple, toTuple (+3) 6 should return (6, 9)
2pts) Using map and the function you just wrote, write a function that takes
a list of Ints and a function (Int -> Int) and returns
a new list of tuples, with the
first element being the original element, and the second being
the result of the transformation.
e.g. [1,2,3] becomes [(1,1), (2, 4), (3, 9)] if the function
passed in returns the square of an element
pls type your code here
thank you
Explanation / Answer
--a function that adds two Ints together. Then create a function, using the first, that adds a number to 10, using partial application add :: Int -> Int -> Int add x y = x + y partialApplication :: Int -> Int partialApplication args = (add args) 10 -- Using partial application/currying, create a function that checks if a Char is a lowercase letter isChar :: Char -> Bool isChar = flip elem ['a'..'z'] --a function that takes another function and an Int and applies it 3 times. func3 :: (Int -> Int -> Int) -> Int -> Int func3 f args = foldl1 f (take 3 $ repeat args) -- Using recursion -- a function that takes a function and a list of Ints, and applies the function to every other element in the list (applies to first, skips the second, and so on), and returns a new list with both the transformed and untransformed elements in place func4 :: (Int -> Int) -> [Int] -> [Int] func4 f lst = func4' f lst 1 [] func4' :: (Int -> Int) -> [Int] -> Int -> [Int] -> [Int] func4' f [] n st = st func4' f lst n st | (n==1) = func4' f (tail lst) 0 (st ++ [f (head lst)]) | otherwise = func4' f (tail lst) 1 (st ++ [head lst]) --a function like zipWith, except that it takes 3 lists and a function that takes 3 parameters, and returns a list with the function applied to each corresponding element in the 3 lists myZipWith :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d] myZipWith f list1 list2 list3 = myZipWith' f list1 list2 list3 [] myZipWith' :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d] -> [d] myZipWith' f [] list2 list3 st = st myZipWith' f list1 [] list3 st = st myZipWith' f list1 list2 [] st = st myZipWith' f list1 list2 list3 st = myZipWith' f (tail list1) (tail list2) (tail list3) (st ++ [f (head list1) (head list2) (head list3)]) -- a function that takes a function (Int -> Int) and an -- Int and returns (Int, Int), where the first element is the Int -- without the function applied to it, and the second is the result of -- applying the function to the Int. That is, if we call the function toTuple, toTuple (+3) 6 should return (6, 9) func5 :: (Int -> Int) -> Int -> (Int,Int) func5 f n = (n,f n) -- Using map and the function --a function that takes --a list of Ints and a function (Int -> Int) and returns --a new list of tuples, with the --first element being the original element, and the second being --the result of the transformation. -- --e.g. [1,2,3] becomes [(1,1), (2, 4), (3, 9)] if the function --passed in returns the square of an element func6 :: [Int] -> (Int -> Int) -> [(Int,Int)] func6 list f = map (x ->(x , f x)) list -- If You have any doubt regarding code then comment below -- If this code is helpful for you then rate it positive. -- Thanks
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.