[Haskell-cafe] Knowledge

jlw501 jlw501 at cs.york.ac.uk
Wed Jan 2 07:32:00 EST 2008


Good point.
By fold/unfold transformation you get the following:

contains = flip elem [Eureka]
=
contains xs e = flip elem xs e [Expose data structures]
=
contains [] e = False
contains (x:xs) e = flip elem (x:xs) e [Instantiate]
=
contains [] e = False
contains (x:xs) e = elem e x:[] || flip elem xs e [Unfold flip one step]
=
contains [] e = False
contains (x:xs) e = elem e x:[] || contains xs e [Fold back to original
defintion]
=
contains [] e = False
contains (x:xs) = e==x || contains xs e [Substitute]

Apparently, the fold/unfold transformation law will always yield an equally
or more efficient computation. So this begs the question...

contains [] e = False
contains (x:xs) = e==x || contains xs e

OR

contains = flip elem



Neil Mitchell wrote:
> 
> Hi
> 
>> > contains :: Eq a => [a]->a->Bool
>> > contains [] e = False
>> > contains (x:xs) e = if x==e then True else contains xs e
>>
>> contains = flip elem
> 
> And even if not using the elem function, the expression:
> 
> if x==e then True else contains xs e
> 
> can be written as:
> 
> x==e || contains xs e
> 
> Thanks
> 
> Neil
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> 

-- 
View this message in context: http://www.nabble.com/Knowledge-tp14423007p14577605.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.



More information about the Haskell-Cafe mailing list