Project Description

    haskell-logo

    Best Haskell Online Programming assignment help . Available  online 24/7 . Please send us an email with the requirement  for the quote. Well commented codes.

    a) Writing polymorphic functions
    Write a polymorphic function that accepts two lists, if the second list is a substring of the first then it should
    return the index of the substring and its length. If it is not a substring then it should return
    (-1,-1). Some
    expected output from this function is shown below:
    Main> substring [1,4,6,2,4,6,7,2,3] [4,6,7]
    (4, 3)
    Main> substring “abbacdaacdcfa” “acdc”
    (7, 4)
    Main> substring [(1,5),(2,4),(6,6),(9,7)] [(2,4),(9,7)]
    (-1, -1)
    b) Writing a higher-order function
    Write a higher-order function that accepts a function and two lists. The higher-order function should return true
    if the given function returns true for every pair of elements in the second list and some substring of the first list.
    For example:
    Main> hsubstring (<) [4,1,2,5,2,4,4] [2,3,6]
    (1, 3)
    Main> hsubstring (<) [4,1,2,5,2,4,4] [2,3,1]
    (-1, -1)
    c) Using higher-order functions
    Write a higher-order function that accepts a function and three lists. The higher-order should determine if the
    second list is a substring of the first. If it is then it should remove it and replace it with the third list, if it is not
    then it should return the original list unchanged. Note that the third list may not be the same length.
    Main> replace (==) [4,1,2,5,2,4,4] [2,5,2] [4,2,1]
    [4,1,4,2,1,4,4]
    Main> replace (==) “Hello World!” “World” “159.202 Class”
    “Hello 159.202 Class!”
    d) Using lambda functions & functional-level definitions
    Define a function that can replace substrings in a sentence regardless of whether they are uppercase or lower
    case. You should use only a lambda function, a functional-level definition and your previous functions.
    Main> replaceString “To be, or not to be, that is the question” “QUESTION” “end”
    “To be, or not to be, that is the end”
    You must put the following comments at the top of your program code and provide the appropriate information.
    — Assignment number, 159.202, 2016 S5
    — Family Name, Given Name, Student ID,
    — Explain what the program is doing . . .
    Hand-in: Submit your script electronically through stream

     

    –Required for ‘toUpper’
    import Data.Char

    –a)
    substring :: (Eq a) => [a] -> [a] -> (Int, Int)
    substring x y = substring’ x y 0

    — susbstring’: auxiliar function that does the substring but has an extra parameter to count the index of the found substring
    — checkEqual a b: checks if all elements from list a are equal to all elements from list b
    substring’ [] _ _ = (-1,-1)
    substring’ (x:xs) (y:ys) c | x==y && checkEqual xs ys = (c, 1 + length ys)
    | otherwise = substring’ xs (y:ys) (c+1)
    where
    checkEqual _ [] = True
    checkEqual (x:xs) (y:ys) | x==y = checkEqual xs ys
    | otherwise = False

    ——————————————————————–
    –b)

    hsubstring :: (a->a->Bool) -> [a] -> [a] -> (Int, Int)
    hsubstring f x y = hsubstring’ f x y 0

    — hsubstring’: auxiliar function that does the hsubstring, but has an extra parametrer to count the index of the found substring
    — checkFunction f a b: checks if f x y is true for elements x from a and y from b
    hsubstring’ _ [] _ _ = (-1, -1)
    hsubstring’ f (x:xs) (y:ys) c | f x y && checkFunction f xs ys = (c, 1 + length ys)
    | otherwise = hsubstring’ f xs (y:ys) (c + 1)
    where
    checkFunction _ _ [] = True
    checkFunction f (x:xs) (y:ys) | f x y = checkFunction f xs ys
    | otherwise = False
    ——————————————————————–
    –c)
    replace :: (a->a->Bool) -> [a] -> [a] -> [a] -> [a]
    replace f a b c | index > -1 = (take index a) ++ c ++ (drop (index+l) a)
    | otherwise = a
    where
    (index, l) = hsubstring f a b

    ——————————————————————–
    –d)
    replaceString :: [Char] -> [Char] -> [Char] -> [Char]
    replaceString a b c = replace (\x y->toUpper x == toUpper y) a b c

     

    Project Details

    • Date November 3, 2016
    • Tags Haskell Programming, Programming

    Leave a reply

    Trustpilot