[Haskell-beginners] While Condition in Haskell

Patrick LeBoutillier patrick.leboutillier at gmail.com
Sun Oct 10 22:09:12 EDT 2010


Lorenzo,

On Sun, Oct 10, 2010 at 4:15 PM, Lorenzo Isella
<lorenzo.isella at gmail.com> wrote:
> ...
> In order to find the length of the longest list in the future which has
> already been seen in the past, I could select the "true" values in the
> output of function
>
> iter_find list i
>
> but this is a waste of CPU: I simply would like a while condition to tell my
> function to stop checking new sublists as soon as it finds one which has not
> occurred in the past and I would like a counter (a kind of i++) telling me
> how many times the process has been iterated.

I'm still a beginner myself, but here's my take on it. Since Haskell
is lazy, the results
will only be generated as they are needed. For example, if you change:

  let b = iter_find list i

for

  let b = takeWhile id $ iter_find list i

Haskell will stop generating the list as soon as it sees a False result.
Then you can take the length of b as your answer.

Patrick


> Any suggestion is helpful.
> Cheers
>
> Lorenzo
>
> -------------------------------------------------------------------
>
> import Data.Ord
>
>
> import Data.List
>
> main :: IO ()
>
> main = do
>
>
>  let list = [4,55,66,77,88,99,12,9,77,88,99,12,-99]
>
>  let i = 9
>
>  let b = iter_find list i
>
>  putStrLn "b is, "
>  print b
>
>
> is_sublist sublist list = sublist `isInfixOf` list
>
>
> gen_fut_list list i j = take j $ drop (i-1) list
>
> gen_past_list list i = take (i-1) list
>
> find_in_list list i j = is_sublist (gen_fut_list list i j) (gen_past_list
> list i)
>
> iter_find list i  = map   (find_in_list list i) [1..n]
>                    where n = (length list) - i +1
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada


More information about the Beginners mailing list