Question

Please use Haskell, thank you! For this question, you will implement (under specific constraints) a recursive...

Please use Haskell, thank you!

For this question, you will implement (under specific constraints) a recursive function in Haskell that takes a list of characters as an argument and returns a list that contains only every third element from the argument list in the same order. As a clarifying example, this function, when passed the argument list “ABCDEFGHIJKLMNO”, should return the list “ CFILO”. It should also be noted that your function must work (i.e., not terminate with an error) even if the number of elements in the argument list is less than three. You may NOT use the !! operator to complete this question, and you may NOT use any function that you haven’t written here (i.e., you may NOT use any built-in functions, but you may use other functions if you write them yourself).

Write this function (including a type declaration) without selector functions (even if you wrote them yourself), using only pattern matching.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

--function definition which accepts a list and returns a list
listThird :: [a] -> [a]
--for no errors return empty list for 0, 1 or 2 elements
listThird [] = []
listThird [x] = []
listThird [_, x] = []
--call the function recursively over the rest of the list which is parted as 1,2, 3rd element and the rest of the list
--undersore represents a single element
listThird (_:_:x:xs) = x:listThird xs

main = do
putStrLn "The input is:" --display this line
-- .. operator automatically creates whole list from A to O
let p = ['A'..'O'] --variable to assign the string
--Any random string can be assigned as below.
--let p = "ABCDEFG"
print(p)
--display the output by calling the function with p as argument
putStrLn "The output is:"
print(listThird p ) --calling a function

Above is the code in the hs file. Second method of defining a random string is shown in the comments above, uncomment and use if needed. remove the two hyphens to uncomment

Sample output

Add a comment
Know the answer?
Add Answer to:
Please use Haskell, thank you! For this question, you will implement (under specific constraints) a recursive...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write below functions in HASKELL functional programming language and remember to include both the (1)...

    Please write below functions in HASKELL functional programming language and remember to include both the (1) function declaration, and (2) function definition. 1. Write a function sumLastPart which, only using library functions, returns the sum of the last n numbers in the list, where n is the first argument to the function. sumLastPart :: Int -> [Int] -> Int

  • Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a...

    Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a function that finds if a given value is present in a linked-list. The function should look for the first occurrence of the value and return a true if the value is present or false if it is not present in the list. Your code should work on a linked-list of any size including an empty list. Your solution must be RECURSIVE. Non-recursive solutions will...

  • All of the problems must be done in Haskell. You may download a local IDE or...

    All of the problems must be done in Haskell. You may download a local IDE or use [this website.][https://repl.it/languages/haskell]. You are not allowed any functions that trivialize a problem! Here are some examples of lists you can test your solutions on: let list1 = [5, 10, 15, 20, 25, 30] let list2 = [50, 100, 150, 200, 250, 300] Write a function that returns the maximum value in a list. Write a function that returns the nth element in the...

  • For the sixth question of this assignment, you must design and implement a function that takes...

    For the sixth question of this assignment, you must design and implement a function that takes a Graph as its first argument and a Node as its second argument and has a list of Nodes (i.e., a [Node] as its return value). The return value must be the list of Nodes traversed by a breadth-first search traversal of the Graph argument, starting from the Node argument, in the order in which they appear during the traversal. When your breadth-first search...

  • Write a recursive function called abb pattern with a single parameter astr, which is a string....

    Write a recursive function called abb pattern with a single parameter astr, which is a string. The function returns True if astr is a string of the form a nb 2n (n a-s followed by 2n b-s, where n is a positive integer) and False otherwise. For example, abb pattern("abb"), abb pattern("aabbbb"), and abb pattern("aaaabbbbbbbb") all return True, but abb pattern("") (parameter is an empty string), abb pattern("abbabb"), and abb pattern("aaabbbbb") all return False. you may not use any built-in...

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • Objectives You will implement and test a class called MyString. Each MyString object keeps track ...

    Objectives You will implement and test a class called MyString. Each MyString object keeps track of a sequence of characters, similar to the standard C++ string class but with fewer operations. The objectives of this programming assignment are as follows. Ensure that you can write a class that uses dynamic memory to store a sequence whose length is unspecified. (Keep in mind that if you were actually writing a program that needs a string, you would use the C++ standard...

  • a) You must write a recursive function that takes something of the form: ([(Int, Int, Int),...

    a) You must write a recursive function that takes something of the form: ([(Int, Int, Int), Int, Int) as an argument and returns something of the form: LE(Int, Int, Int)]] which should be interpreted as a list of lists of 3-tuples of RGB values. You may use helper functions if you wish, but your solution must be recursive. You must document the name of your function at the beginning of your code using a comment like -- foo :: ([(Int,...

  • C programming language: If you malloc, make sure to assert and free the data. please do...

    C programming language: If you malloc, make sure to assert and free the data. please do not call any function you don't write yourself other than: malloc, free, assert, sizeof, scanf/printf families (eg., sprintf, fprintf & printf all OK to use) Write a function whose only argument is the input string. The function should return a new string which consists of only the upper case letters from the original string. The new string should be allocated to use the minimum...

  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT