Church numerals: Difference between revisions

no edit summary
No edit summary
Line 1,781:
 
Note that this is very slow due to that, although function paradigms can be written in Julia, Julia isn't very efficient at doing FP: This is because Julia has no type signatures for functions and must use reflection at run time when the types are variable as here (as to function kind ranks) for a slow down of about ten times as compared to statically known types. This is particularly noticeable for the division function where the depth of function nesting grows exponentially.
 
=={{header|Lambda Calculus}}==
 
I feel we need an example from the original Lambda Calculus. I am here using a dialect that I invented that differs from real untyped Lambda Calculus in two ways:
 
1. Variables can be longer than single letters. In canonical lambda calculus `xy` would be considered an application of `x` to `y`. In my dialect, it is the free variable `xy`. This change is made purely to allow readable variable names.
 
2. I have named expressions introduced by `let`. For example, `let succ = λn.λf.λx.n f (f x)` means that `such` stands for the function on the right hand side. Conceptually, it is just syntactical sugar for `λsucc.( .... everything else that follows ...) (λn.λf.λx.n f (f x)). In particular, it does not introduce conventional recursion into the language.
 
<syntaxhighlight>
# A function that appends an x to its argument
let append = λy.y x
 
let succ = λn.λf.λx.n f (f x)
let 0 = λf.λx.x
let 1 = succ 0
let 2 = succ 1
let 3 = succ 2
let 4 = succ 3
 
let sum = λn.λm.m succ n
let prod = λn.λm.m (sum n) 0
let exp = λn.λm.m (prod n) 1
 
sum 3 4 append y
prod 3 4 append y
exp 3 2 append y
 
sum 3 0 append y
prod 3 0 append y
exp 3 0 append y
 
sum 3 4
 
</syntaxhighlight>
{{out}}
<pre>
y x x x x x x x
y x x x x x x x x x x x x
y x x x x x x x x x
y x x x
y
y x
λf.λx.f (f (f (f (f (f (f x))))))
</pre>
 
Note that since integers in Lambda Calculus are usually defined in terms of Church Numerals, the functions to convert between the two are both the identity function.
=={{header|Lambdatalk}}==
 
19

edits