[Haskell-beginners] While Condition in Haskell

Lorenzo Isella lorenzo.isella at gmail.com
Sun Oct 10 16:15:03 EDT 2010


Dear All,
Definitely I am having some troubles in thinking like an haskeller...
Please consider the snippet at the end of the email. Its aim is rather 
simple: given a list and a value i (position of along the list), it 
defines the "future" as the chunk of the list from i-th element of he 
list to the end of the list, whereas the "past" is anything in the list 
that appears before the i-th element.
I wrote a function which checks whether lists of length 1,2...(till I 
reach the end of the list) in the future (always with respect to 
position i) has already occurred in past.
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.
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






More information about the Beginners mailing list