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

8
  • 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. Commented Jan 24, 2016 at 15:57
  • So that's what is done by now, I need to remove a from the definition. Commented Jan 24, 2016 at 15:59
  • 3
    Try this blunt.herokuapp.com/… Commented Jan 24, 2016 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. Commented Jan 24, 2016 at 16:21
  • 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]. Commented Jan 24, 2016 at 19:00

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.