From: tommo
Subject: Re: Q.  searching lists in Haskell
Date: 
Message-ID: <34D1EF76.DA6C84D8@ncl.ac.uk>
tommo wrote:

> Hi, I am a complete newcomer to haskell (and functional programming in
> general), though am eager to learn about the language in detail.
>
> I have a little problem. I need to write a function that will do the
> following:
>
> Given an integer (i) & an (ordered) list of integers, the function must
> return a tuple (a,b) where
>    a is the position of the first element in the list that contains i
>    b is the position of the last element in the list that contains i
>
> so, the function has type
>
> firstLast :: [Int] -> Int -> Int
>
> Example:
> If the list is [1,2,4,4,4,7,10] and the value is 4, it would return the
> tuple
> (3,5)      N.B elements numbered starting at '1'
>
> One further point, if the integer to be searched for does not contain
> the value, then the function will return the tuple (a,b) where
>    a is the position of the last element in the list that is less than i
>
>    b is the position of the first element in the list that is greater
> than i
>
> Example:
> If the list is [1,2,3,3,3,7,10] and the value is 4, it would return the
> tuple
> (6,5)
>
> Incidentally, a linear search is no good, I need a binary one, although
> once the value has been found the rest of the list can be searched
> linearly to find the lowest/highest position as required.
>
> Hope that covers anything. I would be a great help if someone were able
> to help me with this.
>
> thanks
>
> James Thomson

  P.S. Please mail back
···········@ncl.ac.uk