Skip to main content
added 62 characters in body
Source Link
MaiaVictor
  • 371
  • 1
  • 2
  • 14

Edit: check my answer below for 250 under pure JavaScript.

2852 243 characters using LiveScript (No Regex! Not fully golfed - could be improved)

2852 243 characters using LiveScript (No Regex! Not fully golfed - could be improved)

Edit: check my answer below for 250 under pure JavaScript.

2852 243 characters using LiveScript (No Regex! Not fully golfed - could be improved)

added 10 characters in body
Source Link
MaiaVictor
  • 371
  • 1
  • 2
  • 14

2852 243 characters using LiveScript (notNo Regex! Not fully golfed - could be improved)

2852 243 characters using LiveScript (not fully golfed - could be improved)

2852 243 characters using LiveScript (No Regex! Not fully golfed - could be improved)

added 462 characters in body
Source Link
MaiaVictor
  • 371
  • 1
  • 2
  • 14

LiveScript, using Bruijin Indices: 144 135 1332852 243 characters using LiveScript (not fully golfed - could be improved)

s=L=(I=id;.0==\\)
A=->it.forEach?&&it.0!=\\
V=(c=.toFixed?)
S=(a,b=Ib,f=It=-1,l=1l=0)->map>|L (a=>[\\,S(x)->xa.toFixed&&1,b,t,l+1)];|A a=>(g=x==l&&b||x>l&&f||I;gmap x(->S(a[it],b,t,l)||c x),[0 1]);|a==l+-1=>S(b,f0,l+1)l+-1,a0);||a|l-1<a=>a+t;|_=>a
R=(a,b)->c>|L aa=>[\\,R a.1]|(A a)&&(xL a.0)=>R(S(R(a.0),lR(a.1)->c).1)|_=>a

Test:

a = [\\,[\\,[1 [1 0]]]]
b = [\\,I[\\,(->it+l))[1 [1 [1 0]]]]]
console.log R [a,I) b]
# outputs ["\\",["\\",[1,[1,[1,[1,[1,[1,[1,[1,[1,0]]]]]]]]]]]

Edit: below Which is a more complete version which fills the requirements of the thread, I guess. It has lots of rooms for improvement, though, as I have 2 very similar functions. I am using arrays in the form [f x]3^2=9 to denote application, objects in the form {λ:body} to denote abstractions and integers to denote bruijin indicesas stated on OP.

285 characters If anyone is curious, here is an extended version with some comments:

I=id;λ=# Just type checking
λ = 100
isλ = (.λ?0==λ);P=(
isA = -> it.forEach?);N= && it.0!=λ
isV = (.toFixed?)
S=(t,b=I,f=I,l=0)->|N
# t=>fPerforms t;|λsubstitutions t=>λin trees
# a:S t.λ,trees to perform substitution in
# b: substitute bound variables by this, if != void
# f,l+1;|P: t=>Radd mapthis value to all unbound variables
# l: internal (depth)
S = (xa,b,t=-1,l=0) ->(N 
 x)&&(g=x==l&&b||x>l&&f||I;g x  switch
    | isλ a             => [λ,l)||S x(S a.1, b,f t,l l+1),t]
A=    | isA a             => [(S a.0, b)->|N, a=>[at,b];|λ a=>l), (S a.1,((x b, t, l)]
    | a == l ->S 1        => (S b,I 0, (->it+ll - 1)),( 0) || a
    | l - 1 < a < 100   => a + t
    | _                 => a

# Performs the beta-)reduction
R = (a).λ|P a=>[->
    switch
    | (Aisλ a.0)               => [λ,R a.1),b]1]
R=    | (isA a)-> && (Pisλ a.0)&&(A  => R(S(R (a.0),(R (a.1))||a

Test:

console.log1)
 R [(λ:(λ:[2 [2 [2| 1]]])),_ (λ:(λ:[2 [2 1]]))]                  => a

# Ouputs:Test
a {"λ":{"λ":[2,[2,[2,[2= [λ,[2,[2[1 [1 0]]]]
b = [λ,[2,[2[1 [1 [1 0]]]]]
console.log show R [a,1]]]]]]]]}} b]

Which is 2^3=8, as stated on OP.

LiveScript, using Bruijin Indices: 144 135 133

s=(I=id;(c=(a,b=I,f=I,l=1)->map ((x)->x.toFixed&&(g=x==l&&b||x>l&&f||I;g x,l)||c x,b,f,l+1),a);(a,b)->c a,((x,l)->c b,I,(->it+l)),I)

Edit: below is a more complete version which fills the requirements of the thread, I guess. It has lots of rooms for improvement, though, as I have 2 very similar functions. I am using arrays in the form [f x] to denote application, objects in the form {λ:body} to denote abstractions and integers to denote bruijin indices.

285 characters

I=id;λ=(.λ?);P=(.forEach?);N=(.toFixed?)
S=(t,b=I,f=I,l=0)->|N t=>f t;|λ t=>λ:S t.λ,b,f,l+1;|P t=>R map ((x)->(N x)&&(g=x==l&&b||x>l&&f||I;g x,l)||S x,b,f,l),t
A=(a,b)->|N a=>[a,b];|λ a=>(S a,((x,l)->S b,I,(->it+l-1)),(--)).λ|P a=>[(A a.0, a.1),b]
R=(a)->(P a)&&(A (R a.0),(R a.1))||a

Test:

console.log R [(λ:(λ:[2 [2 [2 1]]])), (λ:(λ:[2 [2 1]]))]
# Ouputs: {"λ":{"λ":[2,[2,[2,[2,[2,[2,[2,[2,1]]]]]]]]}}

Which is 2^3=8, as stated on OP.

2852 243 characters using LiveScript (not fully golfed - could be improved)

L=(.0==\\)
A=->it.forEach?&&it.0!=\\
V=(.toFixed?)
S=(a,b,t=-1,l=0)->|L a=>[\\,S(a.1,b,t,l+1)];|A a=>(map (->S(a[it],b,t,l)),[0 1]);|a==l+-1=>S(b,0,l+-1,0)||a|l-1<a=>a+t;|_=>a
R=(a)->|L a=>[\\,R a.1]|(A a)&&(L a.0)=>R(S(R(a.0),R(a.1)).1)|_=>a

Test:

a = [\\,[\\,[1 [1 0]]]]
b = [\\,[\\,[1 [1 [1 0]]]]]
console.log R [a, b]
# outputs ["\\",["\\",[1,[1,[1,[1,[1,[1,[1,[1,[1,0]]]]]]]]]]]

Which is 3^2=9, as stated on OP.

If anyone is curious, here is an extended version with some comments:

# Just type checking
λ = 100
isλ = (.0==λ)
isA = -> it.forEach? && it.0!=λ
isV = (.toFixed?)

# Performs substitutions in trees
# a: trees to perform substitution in
# b: substitute bound variables by this, if != void
# f: add this value to all unbound variables
# l: internal (depth)
S = (a,b,t=-1,l=0) -> 
    switch
    | isλ a             => [λ, (S a.1, b, t, l+1)]
    | isA a             => [(S a.0, b, t, l), (S a.1, b, t, l)]
    | a == l - 1        => (S b, 0, (l - 1), 0) || a
    | l - 1 < a < 100   => a + t
    | _                 => a

# Performs the beta-reduction
R = (a) ->
    switch
    | (isλ a)               => [λ,R a.1]
    | (isA a) && (isλ a.0)  => R(S(R(a.0),R(a.1)).1)
    | _                     => a

# Test
a = [λ,,[1 [1 0]]]]
b = [λ,,[1 [1 [1 0]]]]]
console.log show R [a, b]
deleted 4 characters in body
Source Link
MaiaVictor
  • 371
  • 1
  • 2
  • 14
Loading
edited body
Source Link
MaiaVictor
  • 371
  • 1
  • 2
  • 14
Loading
added 5 characters in body
Source Link
MaiaVictor
  • 371
  • 1
  • 2
  • 14
Loading
deleted 142 characters in body
Source Link
MaiaVictor
  • 371
  • 1
  • 2
  • 14
Loading
included a more complete version
Source Link
MaiaVictor
  • 371
  • 1
  • 2
  • 14
Loading
added 4 characters in body
Source Link
MaiaVictor
  • 371
  • 1
  • 2
  • 14
Loading
Source Link
MaiaVictor
  • 371
  • 1
  • 2
  • 14
Loading