Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. (30) Using ML (or Scheme or some \"functional-algorithm-language\" like ML or

ID: 3721205 • Letter: 2

Question

2. (30) Using ML (or Scheme or some "functional-algorithm-language" like ML or Scheme), define the following functions: a) evens(X) to create a list consisting of every other element in X starting with the second one (25t, 4rd , etc.) It should return empty list if X does not have at least two items b) last (X) to return the last value in list X c) double (X) to double each item in list X d) cnt (a,X) to count how many times a appears in X. e) less (a,X) to create a list consisting of all integers in X that are less than a.

Explanation / Answer

-- I have used haskell as functional-algorithmic-language and implemented all functions recursively

-- you can install haskell-platform by the command "sudo apt-get install haskell-platform"

-----------------------------------------------------------------------------------------

evens x | length(x) < 1 = []

               | otherwise = evens' x

evens' [] = []

evens' [a] = []

evens' (a:b:x) = b:(evens' x)

-----------------------------------------------------------------------------------------

-- last is inbuilt function in haskell thats why i implemented it as lastt

lastt [] = undefined

lastt [a] = a

lastt (a:x) = lastt x

-----------------------------------------------------------------------------------------

double1 [] = []       -- if you mean double is to repeat each element again

double1 (a:x) = a:a:(double1 x)

double2 [] = [] -- if you mean double is to multiply each element by 2

double2 (a:x) = (2*a):(double2 x)


-----------------------------------------------------------------------------------------

cnt a [] = 0

cnt a (v:x) | a == v = 1+(cnt a x)

                 | otherwise = cnt a x

-----------------------------------------------------------------------------------------

less a [] = []

less a (v:x) | v<a = v:(less a x)

                   | otherwise = less a x

-----------------------------------------------------------------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote