Questions tagged [functional-programming]
This tag is for challenges related to the functional programming paradigm.
48 questions
0
votes
4
answers
336
views
Implement a Monad
A monad is a type that wraps another type, that represents the available operations on the wrapped type. Monads have the following associated operations, where M is ...
10
votes
14
answers
2k
views
Function with memories of its past life
Task
I need a function that accept string and return a function f1.
That f1 function should also accept string and return a <...
24
votes
19
answers
2k
views
Emulate Jelly's tie-scan
The golfing language Jelly has a built-in ƭ called "tie", that cycles through a list of functions every time it's called. For example, ...
3
votes
1
answer
603
views
Implement the simplest functional programming language
The goal of this challenge is to compare how well different programming languages support functional programming, by seeing how much code it takes to implement BLC8, the simplest functional ...
12
votes
3
answers
1k
views
Point-free JavaScript: write a function runner
In this programming-puzzle, your goal is to write the following function in JavaScript, using (essentially) point-free (tacit) programming:
(f) => void(f())
...
10
votes
6
answers
775
views
Write a variadic fixed point combinator
A fixed-point combinator is a higher order function \$\mathrm{fix}\$ that returns the fixed point of its argument function. If the function \$f\$ has one or more fixed points, then $$\mathrm{fix} f=f(\...
28
votes
19
answers
3k
views
Implement the flip-floperator
Ruby has a strange operator, .., called flip-flop (not to be confused with the range operator, which looks the same). Used in a loop, flip-flop takes two conditions ...
16
votes
14
answers
2k
views
Persistence of a number
The persistence of a number \$x = d_1d_2d_3...d_n\$, with \$d_1 \ne 0\$, under some function \$f : \mathbb N_0 \times \mathbb N_0 \to \mathbb N_0\$ is defined as the number of applications of \$f\$ to ...
11
votes
2
answers
928
views
Curry tips for Haskell programmers
Curry is the Language of the Month this month and some users are starting to get into it, so let's get some tips together.
Haskell and Curry are similar in a lot of ways and a lot of tips for Haskell ...
27
votes
44
answers
3k
views
Implement an argwhere function
The \$\text{argwhere}\$ function takes a list of values and a predicate/boolean function as arguments and returns a list of indices where the predicate function returns true in the input list. For ...
18
votes
9
answers
1k
views
Simplify K projections
Background
K functions have a feature called projection, which is essentially partial application of values to a function. The syntax for projections is a natural extension of the regular function ...
7
votes
4
answers
348
views
Ordered, linear, affine, or relevant?
Background
Supplementary reading 1, Supplementary reading 2
Linear lambda calculus is a limited form of lambda calculus, where every bound variable must be used exactly once. For example, ...
19
votes
16
answers
2k
views
Unfold a list using a function
Task
Haskell's and Scala's standard libraries have an unfold function that builds a list from an initial state s and a function <...
17
votes
2
answers
1k
views
We all know how to SKI, but can you BCKW?
Background
Lambda calculus is a model of computation using lambda terms.
A variable \$x\$ is a lambda term.
If \$E\$ is a lambda term, the lambda abstraction \$\lambda x. E\$ is a lambda term.
If \$...
23
votes
46
answers
3k
views
Implement a cleave function
Think of cleave as the conceptual inverse of map. If map applies a function to each number in a list...
map([1, 2, 3], x -> x * 5) -> ...
9
votes
3
answers
525
views
Implement Fix2 combinator
Background
The fixed-point combinator \$\textsf{fix}\$ is a higher-order function that computes the fixed point of the given function.
$$\textsf{fix}\ f = f\ (\textsf{fix}\ f)$$
In terms of ...
15
votes
10
answers
1k
views
Simplify K combinatory logic expression
Background
Combinatory logic is a system where a term is written using a finite set of combinators and function application between terms, and reduction rules are defined for each combinator. The well-...
18
votes
2
answers
491
views
Solve the halting problem for S combinatory logic
Background
Combinatory logic is a system where a term is written using a finite set of combinators and function application between terms, and reduction rules are defined for each combinator. The well-...
26
votes
39
answers
2k
views
Apply at indices
Given:
a blackbox function \$f : \mathbb N \to \mathbb N\$,
a list of positive integers \$L\$, and
a list of indices \$I\$,
apply \$f(x)\$ to the elements of \$L\$ at the indices specified in \$I\$.
...
37
votes
52
answers
3k
views
Implement an Over function
Over is a higher-order function in multiple languages such as APL (⍥). It takes 2 functions and 2 values as arguments, applies the first function to both values, ...
25
votes
43
answers
4k
views
Implement a zipwith function
zipwith is a functional construct that takes three arguments: one binary function and two lists of the same length, and returns a single list where each element is ...
23
votes
18
answers
5k
views
I don't like curry
I don't like curry. Help me reverse the effects of this evil question - Make me some curry - by uncurrying functions.
Task
Given a blackbox curried function, output its uncurried equivalent.
The ...
15
votes
9
answers
2k
views
Church Subtraction
Church Subtraction
Lambda calculus has always been a fascination of mine and the emergent behaviors of passing functions into each other is delightfully complex. Church numerals are representations ...
36
votes
20
answers
8k
views
Church Booleans
Church booleans
A Church boolean is a function that returns x for true and y for false where ...
5
votes
2
answers
1k
views
Point-free madness
This challenge is about Haskell point-free style polynomial functions.
Although you don't need to know Haskell language to do this challenge, Haskellers might have an advantage here.
Point-free ...
22
votes
14
answers
2k
views
Dirichlet Convolution
The Dirichlet convolution is a special kind of convolution that appears as a very useful tool in number theory. It operates on the set of arithmetic functions.
Challenge
Given two arithmetic ...
1
vote
2
answers
328
views
Map an array of functions to their return values in point-free style
Introduction
I have some JavaScript code that uses Array.prototype.map to map an array of functions fns to their return values:
...
18
votes
15
answers
2k
views
Split a list into sized chunks, but not counting items failing predicate
Motivation: Sometimes certain items in a list don't count towards your totals. For example, counting plane passengers in rows, where babies sit on a parent's laps.
Challenge: write a program to split ...
29
votes
23
answers
4k
views
Make me some curry
Having a function f that takes arguments x1, x2, …, xn
&...
42
votes
81
answers
9k
views
How many arguments were passed?
Using your language of choice, write a function that takes a variable number of arguments and returns the number of arguments it was called with.
Specifics:
Your language needs to support variadic ...
18
votes
20
answers
1k
views
Find relevant digit groupings
Recently, my reputation was 25,121. I noticed that each digit grouping (i.e. the numbers separated by commas) was a perfect square.
Your challenge is, given a non-...
23
votes
28
answers
2k
views
Generalized matrix trace
Inspiration.
Given (by any means):
A two-argument (or single argument consisting of a two-element list) black box function, f: ℤ+ × ℤ+ → ℤ+ (input and output are 1,...
18
votes
4
answers
1k
views
Tuple addition in pointfree
What is the shortest way we can express the function
f(a,b)(c,d)=(a+c,b+d)
in point-free notation?
pointfree.io gives us
...
25
votes
32
answers
3k
views
Find a Fixed Point
Given an integer \$x_1\$ and some black box function \$f: ℤ → ℤ\$ find a fixed point of \$f\$ in the sequence defined by \$x_{k+1} := f(x_k)\$.
Details
A value \$x\$ is said to be a fixed point of \$...
16
votes
3
answers
1k
views
Are there any functional programming languages designed for code-golfing?
Are there any functional programming languages designed for code golfing? I know that golfscript and CJam fulfill the same category for stack based, but I couldn't find a functional code golfing ...
58
votes
45
answers
5k
views
Arbitrary-length currying
Write a function, f, that takes in a positive integer and returns a function.
The new function returned should be identical to f...
22
votes
1
answer
2k
views
Convert λ-expressions to SK-expressions
The λ-calculus, or lambda calculus, is a logical system based on anonymous functions. For example, this a λ-expression:
λf.(λx.xx)(λx.f(xx))
However, for the ...
37
votes
96
answers
5k
views
Implement Takewhile
Introduction and Credit
Today without a fancy prelude: Please implement takewhile.
A variation of this (on a non-trivial data structure) was an assignment at my ...
25
votes
15
answers
2k
views
Make a long type signature
Challenge
Find an expression, at most 100 bytes long, with the longest type signature.
Rules
Any statically typed language with type inference is allowed
The type must be non-ambiguous, but otherwise ...
0
votes
3
answers
222
views
Define map in the fewest tokens [closed]
map is a very basic yet important function in functional programming.
All FP programming languages have it built-in but it is always fun to see how shortly you can ...
25
votes
11
answers
1k
views
Implement functional programming paradigms
Your company is just getting started on a project, and for the first time you decided to go use a functional programming code-style. However your boss is really diffident and doesn't want to use built-...
8
votes
6
answers
2k
views
Most concise way to fold tree structures
Background (F#)
Let there be trees:
type Tree<'T> = Node of 'T * Tree<'T> list
Now lets fold them nicely with a function called...
...
11
votes
4
answers
1k
views
Write a Shift Interpreter
EDIT: As some of you suspected, there was a bug in the official interpreter: the order of composition in . was reversed. I had two versions of the interpreter, and ...
-2
votes
3
answers
314
views
Compare array of decimal sum of Integers and exact fractional part
Develop a program which takes two arrays of decimal numbers, and compare the sum of whole numbers only and the decimal part. If the sums of the whole numbers are the same, and the decimal parts of ...
9
votes
5
answers
437
views
Write biSp in point-free form using as few terms as possible
The Haskell function biSp has type signature
...
21
votes
3
answers
3k
views
Shortest a -> b -> (a -> b) function in Haskell
I got the following question at a test:
Write a function f with the following type a -> b -> (a -> b). ...
5
votes
12
answers
620
views
Write a function that reduces compositions of linear operators
You are given two functions \$g(x)\$ and \$h(x)\$, each of which takes an integer \$x\$ and returns the number \$ax + b\$ (where \$a\$ and \$b\$ are integers defined in the function).
Your task is to ...
13
votes
9
answers
791
views
Golfed fixed point combinator
Write a fixed point combinator in as few characters as possible, in the language of your choice.
free form (i.e., whatever's shortest): whole program, actual function, code snippet
you may not use ...