Task description below. In my opinion, only process1 is suitable, but this answer is incorrect. What other features are suitable?

Below are the implementations of the process :: (b -> d) -> [(a, b, c)] -> [d] function, which takes a handler function as the first argument, and a list of triples as the second. The process function should apply a handler to the middle element of every third triple from the list, counting from its head. Return this function should a list of processing results, for example

GHCi> process (^ 2) [(1,1,1), (1,2,1), (1,3,1), (1,4,1), (1,5,1), (1 , 6,1), (1,7,1)] [9,36] Select those versions of the implementation of the process function that actually do this work on any lists.

 process0 f [] = []
 process0 f [t1] = []
 process0 f (t1: t2: (x, y, z): ts) = fy: process0 f ts

 process1 f [] = []
 process1 f (t: []) = []
 process1 f (t1: t2: []) = []
 process1 f (t1: t2: (x, y, z): ts) = fy: process1 f ts

 process2 f [] = []
 process2 f (t1: t2: (x, y, z): ts) = fy: process2 f ts
 process2 f [t] = []
 process2 f [t1, t2] = []

 process3 f [t] = []
 process3 f (t1: t2: (x, y, z): ts) = fy: process3 f ts
 process3 f (t1: t2: ts) = []
 process3 f [] = []

 process4 f (t1: t2: (x, y, z): ts) = fy: process4 f ts
 process4 f ts = []

    3 answers 3

    If you are limited only by the scope of your task, and the fact that process1 is not correct, then process4 will do:

    process4 f (t1 : t2 : (x,y,z) : ts) = fy : process4 f ts process4 f ts = [] 

    Such a record is more concise and does not require additional checks, respectively, is more correct than process1, even if you did not specify the conditions of the problem.

      • process0 - the variant of the list of two elements is not considered
      • process1 - considered: empty, with one element, with two, and the main option - everything is OK
      • process2 - considered empty, with one too, with two too, the main one is - everything is OK, the order here will not be affected
      • process3 - a list with one, the main option, a list of two or more (not included in the main one), and empty, i.e. all options - everything is ok
      • process4 - considered the main option and all the rest - everything is OK

      So all are suitable except process0.

       λ> let tests n = [[(x, x, x) | x <- [1..l]] | l <- [1..n]] λ> map (process0 id) (tests 4) [[],[],*** Exception: <interactive>:3:5-88: Non-exhaustive patterns in function process0 λ> map (process1 id) (tests 4) [[],[],[],[3],[3]] λ> map (process2 id) (tests 4) [[],[],[],[3],[3]] ... 

        Very strange you have formulated the question. But if I correctly understood what you need, then it suffices to write:

         takeEvery n list = map snd $ filter ((==0) . (`mod` n) . fst) $ zip [1..] list process f list = map (\(_, x, _) -> fx) $ takeEvery 3 list 
        • Most likely, ironfrog has a test task, and you need to choose the correct option from the ones offered. Therefore, even if you have the correct answer, this will not help the author of the question) - insolor