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 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...

  • 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....

  • 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...

  • 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,...

  • on.ca i.cmd med to u nunaown C eSA COwPA 20 or 2. Write a function that...

    on.ca i.cmd med to u nunaown C eSA COwPA 20 or 2. Write a function that takes a single list of characters as argument and returns a new list of integers as a return value. The list returned must contain the ASCII codes of every uppercase character in the original list in the reverse order from the order they appeared in the original list. You are not permitted to use negative indexing (i.e., the numbers you write in the square...

  • write the following code in python 3.2+. Recursive function prefered. Thank you! Define a function named...

    write the following code in python 3.2+. Recursive function prefered. Thank you! Define a function named q3() that accepts a List of characters as a parameter and returns a Dictionary. The Dictionary values will be determined by counting the unique 3 letter combinations created by combining the values at index 0, 1, 2 in the List, then the values at index 1, 2, 3 and so on. The q3() function should continue analyzing each sequential 3 letter combination and using...

  • use python to solve this problem Problem 3- contains.py: Sub-list containment You will write a function...

    use python to solve this problem Problem 3- contains.py: Sub-list containment You will write a function to determine whether a given sub-list" s appears within another given list 1. The problem will guide you through the problem breakdown. Essentially, first you can solve the easier problem if the "sub-list" s appears within 1 at a specific position i. Then Practice goal: Guided practice with modular thinking and programming. Problem 3a Partial equality [5 points] Write a function contains_at that accepts...

  • Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement...

    Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement some list functionalities on an ArrrayList of generic type using type parameters in method definition Our goal for this lab is to continue using the divide and conquer approach of splitting a list into its head and tail and keep recursing on the tail (which happens to be smaller). However, instead of trying the approach on a string (a list of characters) we would...

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
ADVERTISEMENT