[Haskell-beginners] While Condition in Haskell

Thomas Miedema thomasmiedema at gmail.com
Mon Oct 11 07:46:54 EDT 2010


Hi Lorenzo,

what Patrick said was correct. Here's your program a bit more the Haskell
way. Also see:
http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Data-List.html


import Data.List

main = do
  let (past, future) = splitAt 8 [4,55,66,77,88,99,12,9,77,88,99,12,-99]
      subfutures = tail . inits $ future
  print . takeWhile (`isInfixOf` past) $ subfutures


Regards,
Thomas


On Mon, Oct 11, 2010 at 4:09 AM, Patrick LeBoutillier <
patrick.leboutillier at gmail.com> wrote:

> 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
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20101011/c6b1d7df/attachment.html


More information about the Beginners mailing list