0

I have a function here

(map (\ (a, b) -> dir a b) $ routes 

where routes is a list of tuples and it contains

routes = [...
           ("graph-fb", seeOther redirectUrlGraphEmail $ toResponse ""),
           ("post-fb", seeOther redirectUrlGraphPost $ toResponse ""),
          ...]

Here is the question: when I call this function to apply dir on each tuple, which function is going to be returned as b in dir a b first, the function seeOther redirectUrlGraphEmail or seeOther redirectUrlGraphEmail $ toResponse ""?

Mia.M
  • 97
  • 1
  • 1
  • 8
  • 3
    Why does it matter? Just from the type of `map`, we know `dir` can't behave differently depending on what order it is fed arguments. This is not just a rhetorical question: I suspect if you say why you think it matters, you will reveal some confusion about Haskell that we can help clear up for you. – Daniel Wagner Aug 31 '16 at 17:57
  • `routes` is not a function; it's a list. – dfeuer Aug 31 '16 at 20:36
  • Thanks @dfeuer for mentioning that. I already corrected this in the question. – Mia.M Sep 01 '16 at 06:03
  • And to answer @DanielWagner's question, I am confused because in haskell `$` has the lowest precedence. In this case, since first `redirectUrlGraphEmail` will be fed into `seeOther` as an argument, what will happen next is to return the value as `b` or is to apply `$` on `toResponse ""` ? – Mia.M Sep 01 '16 at 07:46

2 Answers2

2

Tuples items are separated by , which means that in your example for graph-fb,

a == "graph-fb"
b == seeOther redirectUrlGraphEmail $ toResponse ""

The first function call to resolve will be toResponse "", and that will be fed into seeOther as the second parameter. The result of this will then have the label b inside the mapping function.

Chad Gilbert
  • 36,115
  • 4
  • 89
  • 97
  • I understand what you said, but isn't `$` has the lowest precedence in haskell which makes `seeOther` to call `redirectUrlGraphEmail` first? – Mia.M Sep 01 '16 at 07:48
  • `$` has the lowest precedence compared to other infix operators. That whole bit on the right of the comma only contains a single infix operator, so it will be "bundled up" and labeled as `b` in the mapping function. – Chad Gilbert Sep 01 '16 at 10:19
0

There's no way to tell from that code. Even ignoring the compiler's freedom to reorganize your program in various dramatic ways, the usual approach to Haskell evaluation is lazy, which you can think of as demand-driven, case-driven, and (ultimately) I/O-driven. Whoever actually inspects the results determines the evaluation order.

dfeuer
  • 48,079
  • 5
  • 63
  • 167