Search code examples
functionhaskellcomposition

haskell, function composition


I need some help to solve a task on function composition in Haskell. I need to write a function that given an Integer n and a list of inner lists of elements, returns the list of n-th element in each inner list. So it would be like: select 2 [[2,3,4],[5,6],[9,9,9]] = [3,6,9]. The thing is, I need to write it using function composition so it should look like select = .... In other words, I want to make this point-free.

For now, I have the following:

select::Int->[[Int]]->[Int]
select a = map $ head. reverse. take a 

I'm stuck with it, I don't know how to remove those a from the first and only clause. Can anybody help me with this?:)


Solution

  • Based on what you currently have, you can use select = map . ((head . reverse) .) . take, you could also simplify this to select = map . (last .) . take. An alternative would be to use select = map . flip (!!) . subtract 1.

    You can use the pointfree package to derive pointfree versions of functions automatically.

    Generally I would advise against this though. Functions that have multiple parameters become quite obfuscated when they are defined in pointfree style.