I'm currently reading Algorithm Design with Haskell by Richard Bird & Jeremy Gibbons, the Saddleback search algorithm is given in chapter 4 after binary search, The problem given for motivation is :
given f:N x N -> N is strictly increasing in both arguments, and a value t find all the pairs (x,y) such that f(x,y)=t.
The first definition is given by search f t =[(x,y) | x ← [0..t],y ← [0..t],t == f(x,y)],which searches starting from the origin at the bottom-left corner to the top-right corner, (the indices are increasing from bottom to top and from left to right), then the book proceeds for improvements.
The second definition is given by search f t =[(x,y) | x ← [0..t], y ← [t,t −1..0],t == f(x, y)],
The book quotes this definition with
"The first improvement is to start at the top-left rather than the bottom-left corner", then the book continues till it becomes Theta(m+n) for a matrix with dimensions n x m.
The final algorithm looks like this
search f t = searchIn (0,t)
where searchIn (x, y) | x>t ∨ y<0 =[]
| z<t = searchIn (x+1, y)
| z == t = (x, y):searchIn (x+1, y−1)
| z>t = searchIn (x,y−1)
where z = f(x,y)
Why is it the case? What's wrong with starting from the origin (0,0)?, also why take x-coordinate to be increasing and y-coordinate to be decreasing, or vice versa (I've seen it on leetCode with x decreasing and y increasing), why not take both coordinates in increasing direction?


