[Haskell-cafe] beginner's problem about lists
fis at wiwi.hu-berlin.de
Tue Oct 10 08:49:48 EDT 2006
On Tue, Oct 10, 2006 at 08:10:44PM +0800, falseep at gmail.com wrote:
> To: haskell-cafe at haskell.org
> From: falseep at gmail.com
> Date: Tue, 10 Oct 2006 20:10:44 +0800
> Subject: [Haskell-cafe] beginner's problem about lists
> Hi all,
> I'm trying to implement a function that returns the shorter one of two given
> something like
> shorter :: [a] -> [a] -> [a]
> such that shorter [1..10] [1..5] returns [1..5],
> and it's okay for shorter [1..5] [2..6] to return either.
> Simple, right?
> However, it becomes difficult when dealing with infinite lists, for example,
> shorter [1..5] (shorter [2..] [3..])
> Could this evaluate to [1..5]? I haven't found a proper implementation.
> Again it's ok for shorter [2..] [3..] to return whatever that can solve
> the above problem correctly.
> An infinite list could work, I guess, but I don't know how.
a function that takes two lists and decides whether one of them is
finite or not , without being given further information on the lists,
does not exist.
you could add a third argument 'inifinity :: Int' that sets the minium
length of all lists that are considered infinite. this is where your
function could stop looking for an end in either of the two
shorter infinity = reverse . f infinity  
f 0 ar al _ _ = ar
f _ ar al  _ = ar
f _ ar al _  = al
f (i+1) ar al (x:xs) (y:ys) = f i (x:ar) (y:al) xs ys
or you could specify your function to be callable with at most one
i guess that's all you can do?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20061010/6628c426/attachment-0001.bin
More information about the Haskell-Cafe