
19 | Trans + Aroace | Programming and Drawing and Stuffs
154 posts
I Accidentally Messed Up The Definition Of Foldl''''it Should Befoldl'''' K Z0 Xs = Let FoldFunction
I accidentally messed up the definition of foldl'''' it should be foldl'''' k z0 xs = let foldFunction = foldr (flip (.)) $ map (flip k) xs in foldFunction z0 Oh yeah an explanation: essentially, both fold functions can be viewed as:
foldr f z [x₀, x₁, x₂] = f x₀ (f x₁ (f x₂ z)) foldl f z [x₀, x₁, x₂] = f (f (f z x₀) x₁) x₂
The Haskell foldl is defined via foldr. It does it mainly by turning every element in xs into a function. this is most evident when you rewrite it as
foldl_ :: (b -> a -> b) -> b -> [a] -> b foldl_ f z0 xs = let accF = foldr (flip (.)) id $ map (flip f) xs in accF z0
And it becomes even more obvious in a β reduction some extra definitions (.) f g = \x -> f (g x)
flip f x y = f y x
-- Initial foldl f z [x₀, x₁, x₂] -- foldl definition let accF = foldr (flip (.)) id $ map (flip f) [x₀, x₁, x₂] in accF z -- map reduction let accF = foldr (flip (.)) id $ [flip f x₀, flip f x₁, flip f x₂] in accF z -- foldr reduction let accF = flip (.) (flip f x₀) (flip (.) (flip f x₁) (flip (.) (flip f x₂) id)) in accF z -- flip (.) reduction let accF = (.) ((.) ((.) id (flip f x₂)) (flip f x₁)) (flip f x₀) in accF z -- (.) reduction let accf = \x -> (\x -> (\x -> id ((flip f x₂) x)) ((flip f x₁) x)) ((flip f x₀) x) in accF z -- β reduction let accf = \x -> id ((flip f x₂) ((flip f x₁) ((flip f x₀) x))) in accF z -- flip reduction let accf = \x -> id (f (f (f x x₀) x₁) x₂ ) in accF z -- id reduction let accf = \x -> f (f (f x x₀) x₁) x₂ in accF z -- accf reduction f (f (f z x₀) x₁) x₂
and we end up with the original `foldl f z [x₀, x₁, x₂] = f (f (f z x₀) x₁) x₂` definition
Stuff like these are what makes me both love and hate Haskell

-
the-moon-exalted liked this · 1 year ago
-
perigordtruffle reblogged this · 1 year ago
-
lady-inkyrius reblogged this · 1 year ago
-
lady-inkyrius liked this · 1 year ago
-
perigordtruffle reblogged this · 1 year ago
-
perigordtruffle reblogged this · 1 year ago
More Posts from Perigordtruffle
I want clothes that is just one really long run-on sentence of a story or something told very energetically. Not just as a design on the front but wraps around the whole body and maybe it starts with one large "One time I..." to grab initial attention.
newly awakened shapeshifter, who was once a guy, goes through the "woke up as a girl" routine before figuring out their new power and tries to change back with their mental image of their old self in mind, only to look at the mirror and see a blank face staring back
u ever see someone with extremely fucked up views (or actions) and think wowww if a couple of things in my life went the tiniest bit differently that would have been me