Permalink
Browse files

Alternative foldr implementation/type.

  • Loading branch information...
1 parent 0db831a commit 98193156ac7010dfd858b7ef23ba5c64acc53905 @tomlokhorst committed Aug 22, 2010
Showing with 3 additions and 0 deletions.
  1. +3 −0 src/Generic/Data/List.hs
@@ -40,6 +40,9 @@ map f = foldr (\y ys -> f y `cons` ys) nil
foldr :: (RecFunC j, ListC j) => (j a -> j b -> j b) -> j b -> j [a] -> j b
foldr f b xs = fix (\r -> lam (list b (\y ys -> f y (r `app` ys)))) `app` xs
+foldr' :: (RecFunC j, ListC j) => j (a -> b -> b) -> j b -> j [a] -> j b
+foldr' f b xs = fix (\r -> lam (list b (\y ys -> f `app` y `app` (r `app` ys)))) `app` xs
+
replicate :: (Eq j a, BoolC j, RecFunC j, ListC j, Num j a) => j a -> j b -> j [b]
replicate n a = fix (\r -> lam (\y -> bool nil (a `cons` (r `app` (y - 1))) (y == 0))) `app` n

4 comments on commit 9819315

@sjoerdvisscher

foldr can now be implemented with foldr' and lam2?

@tomlokhorst
Owner

Yes, indeed. Although, I'm not sure if the foldr with the current type should even exist. The second foldr' seems more consistent with the rest of the AwesomePrelude.

@sebastiaanvisser
Collaborator

But still, like I commented on the e-mail, the idea behind the AP was to have Haskell-style functions produce JavaScript code. That's why foldr takes a Haskell function of "j" values. Foldr' takes a "j"-style function and produces a Haskell-style function. That is weird.

BTW, this is all due to the fact that Haskell has crappy syntax overloading abilities. Like all languages.

@tomlokhorst
Owner

I guess your right, now I can't really remember what code I was looking at when I imagined j (a -> b) was better style then (j a -> j b).

Btw, in defense of Haskell; It only has crappy syntax for overloading function abstraction and application (as well as for data types), but it works great for numeric literals! That's at least something more than most languages.

Please sign in to comment.