[Haskell-beginners] Re: folds again -- myCycle

7stud bbxx789_05ss at yahoo.com
Thu Mar 19 15:32:19 EDT 2009


Will Ness <will_n48 <at> yahoo.com> writes:
> In our case, having 
> The access in (head ys) gets traslated in
> 
> head ys = head (ys ++ [1,2,3]) = head ((ys ++ [1,2,3]) ++ [1,2,3])
> 
> but for the other defintion 
> 
> let zs = [1, 2, 3] ++ zs
> 
> it's
> 
> head zs = head([1, 2, 3] ++ zs) = head( (1:[2,3]) ++ zs) 
> = head( 1:([2,3]++zs) ) = 1
> 
> according to the defintion of (++),
> 
> (x:xs)++ys = x:(xs++ys)
> 
> Were we to use the foldr definition, it'll get rewritten just the same, using 
> the foldr definition (as long as it's not the left-recursive defintion):
> 
> foldr f z (a:as) = a `f` foldr f z as
> 
> let ws = foldr (:) [1,2,3] ws
> 
> head ws = head (foldr (:) [1,2,3] ws) 
> = head (foldr (:) [1,2,3] (foldr (:) [1,2,3] ws))
> 
> because 'ws' is getting matched against (a:as) pattern in foldr definition, so 
> is immediately needed, causing INFINITE looping. BUT
>

I'm confused by your statement that ws is getting matched against the
(a:as) pattern in the foldr definition, and therefore it is immediately 
evaluated.   I didn't think the let expression:

> let ws = foldr (:) [1,2,3] ws

would cause infinite looping.

Wouldn't it be the head pattern that is being matched, and therefore head
triggers the evaluation?  Although, I'm not seeing a pattern match here:

>head (x:xs) = x
>head (foldr (:) [1,2,3] (foldr (:) [1,2,3] ws))


..
...
...
..
..
,,,
...
....






More information about the Beginners mailing list