Questions tagged [combinators]

A combinator is a higher-order function that uses only function application and earlier defined combinators to define a result from its arguments.

176 questions
434
votes
18 answers

What is a Y-combinator?

A Y-combinator is a computer science concept from the “functional” side of things. Most programmers don't know much at all about combinators, if they've even heard about them. What is a Y-combinator? How do combinators work? What are they good…
134
votes
4 answers

foldl versus foldr behavior with infinite lists

The code for the myAny function in this question uses foldr. It stops processing an infinite list when the predicate is satisfied. I rewrote it using foldl: myAny :: (a -> Bool) -> [a] -> Bool myAny p list = foldl step False list where …
titaniumdecoy
  • 18,900
  • 17
  • 96
  • 133
95
votes
1 answer

Explanation of combinators for the working man

What is a combinator?? Is it "a function or definition with no free variables" (as defined on SO)? Or how about this: according to John Hughes in his well-known paper on Arrows, "a combinator is a function which builds program fragments from…
Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
92
votes
11 answers

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (\x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions. Any suggestions?
dinsim
  • 2,395
  • 5
  • 24
  • 32
84
votes
7 answers

foldl is tail recursive, so how come foldr runs faster than foldl?

I wanted to test foldl vs foldr. From what I've seen you should use foldl over foldr when ever you can due to tail reccursion optimization. This makes sense. However, after running this test I am confused: foldr (takes 0.057s when using time…
Ori
  • 4,961
  • 10
  • 40
  • 39
57
votes
8 answers

Good explanation of "Combinators" (For non mathematicians)

Anyone got a good explanation of "combinators" (Y-combinators etc. and NOT the company)? I'm looking for one for the practical programmer who understands recursion and higher-order functions, but doesn't have a strong theory or math…
interstar
  • 26,048
  • 36
  • 112
  • 180
45
votes
7 answers

In Haskell performing `and` and `or` for boolean functions

I just wrote the following two functions: fand :: (a -> Bool) -> (a -> Bool) -> a -> Bool fand f1 f2 x = (f1 x) && (f2 x) f_or :: (a -> Bool) -> (a -> Bool) -> a -> Bool f_or f1 f2 x = (f1 x) || (f2 x) They might be used to combined the values of…
John F. Miller
  • 26,961
  • 10
  • 71
  • 121
40
votes
2 answers

Parallel map in haskell

Is there some substitute of map which evaluates the list in parallel? I don't need it to be lazy. Something like: pmap :: (a -> b) -> [a] -> [b] letting me pmap expensive_function big_list and have all my cores at 100%.
Clark Gaebel
  • 17,280
  • 20
  • 66
  • 93
39
votes
4 answers

How does Data.MemoCombinators work?

I've been looking at the source for Data.MemoCombinators but I can't really see where the heart of it is. Please explain to me what the logic is behind all of these combinators and the mechanics of how they actually work to speed up your program in…
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
38
votes
2 answers

Y combinator discussion in "The Little Schemer"

So, I've spent a lot of time reading and re-reading the ending of chapter 9 in The Little Schemer, where the applicative Y combinator is developed for the length function. I think my confusion boils down to a single statement that contrasts two…
planarian
  • 2,047
  • 18
  • 18
33
votes
6 answers

Combinatory method like tap, but able to return a different value?

I'm going through a phase of trying to avoid temporary variables and over-use of conditional where I can use a more fluid style of coding. I've taken a great liking to using #tap in places where I want to get the value I need to return, but do…
d11wtq
  • 34,788
  • 19
  • 120
  • 195
33
votes
14 answers

What are some interesting uses of higher-order functions?

I'm currently doing a Functional Programming course and I'm quite amused by the concept of higher-order functions and functions as first class citizens. However, I can't yet think of many practically useful, conceptually amazing, or just plain…
29
votes
5 answers

What are zygo/meta/histo/para/futu/dyna/whatever-morphisms?

Is there a list of them with examples accessible to a person without extensive category theory knowledge?
Fixpoint
  • 9,619
  • 17
  • 59
  • 78
23
votes
2 answers

What are super combinators and constant applicative forms?

I'm struggling with what Super Combinators are: A supercombinator is either a constant, or a combinator which contains only supercombinators as subexpressions. And also with what Constant Applicative Forms are: Any super combinator which is not a…
Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
22
votes
4 answers

What is the difference between liftM and mapM in Haskell

What is the difference between the functions liftM and mapM?
Luke
  • 5,771
  • 12
  • 55
  • 77
1
2 3
11 12