0

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?:)

Paweł Poręba
  • 1,084
  • 1
  • 14
  • 37
  • Not sure I understand the question. You will have to give the integer n (guess that is a in your code?), to one of the combined functions. – Koebmand STO Jan 24 '16 at 15:57
  • So that's what is done by now, I need to remove `a` from the definition. – Paweł Poręba Jan 24 '16 at 15:59
  • Doesn't make it anymore clear what you are trying to accomplish, sorry. – Koebmand STO Jan 24 '16 at 16:03
  • 3
    Try this https://blunt.herokuapp.com/#input=select%20a%20%3D%20map%20%24%20head.%20reverse.%20take%20a%20 – Mephy Jan 24 '16 at 16:13
  • 2
    If you are learning, don't try to write a point-free function. Write a normal function with arguments, get it working, then *transform* it to point-free style. – crockeea Jan 24 '16 at 16:21
  • I guess it's the exercise to write it ;) – Random Dev Jan 24 '16 at 16:30
  • 1
    Side note: You give the example: `select 2 [[2,3,4],[5,6],[9,9,9]] = [3,6,9]`, which implies that the first element of the list is at index 1. This is non-standard in Haskell (and most programming languages)—typically lists are indexed starting at 0. To me, this would be the expected behavior: `select 1 [[2,3,4],[5,6],[9,9,9]] = [3,6,9]`. – MaxGabriel Jan 24 '16 at 19:00
  • @MaxGabriel The task indicated to start at 1. To me, this wouldn't be written point-free. – Paweł Poręba Jan 24 '16 at 19:42

1 Answers1

3

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.

Will Sewell
  • 2,593
  • 2
  • 20
  • 39