diff --git a/src/lambda-calculus.js b/src/lambda-calculus.js index 807410b..e68604d 100644 --- a/src/lambda-calculus.js +++ b/src/lambda-calculus.js @@ -221,10 +221,12 @@ function parse(code) { function name(i) { if ( code[i]==='_' ) { while ( identifierChars.test( code[i] || "" ) ) i++; + if ( code[i]==='?' ) i++; return [sp(i),"_"]; } else if ( letters.test( code[i] || "" ) ) { let j = i+1; while ( identifierChars.test( code[j] || "" ) ) j++; + if ( code[j]==='?' ) j++; return [sp(j),code.slice(i,j)]; } else return null; diff --git a/tests/basics-binary-scott/solution.txt b/tests/basics-binary-scott/solution.txt index de9984f..174711b 100644 --- a/tests/basics-binary-scott/solution.txt +++ b/tests/basics-binary-scott/solution.txt @@ -56,12 +56,12 @@ shiftL = \ n . n 0 I I # mind that a shiftL in LE is a divisi dbl = \ n . n 0 (K (shiftR0 n)) (K (shiftR0 n)) -isStrictZero = \ n . n True (K False) (K False) # disallow padding zeroes # O(1) -isZero = \ n . n True isZero (K False) # allow padding zeroes # amortised O(2), so don't worry too much +isStrictZero? = \ n . n True (K False) (K False) # disallow padding zeroes # O(1) +isZero? = \ n . n True isZero? (K False) # allow padding zeroes # amortised O(2), so don't worry too much pad = \ n . n (shiftR0 0) (B shiftR0 pad) (B shiftR1 pad) -unpad = \ n . n 0 ( \ z . ( \ unpadZ . isStrictZero unpadZ (shiftR0 unpadZ) 0 ) (unpad z) ) (B shiftR1 unpad) -isPadded = \ n . n False ( \ z . z True (K (isPadded z)) (K (isPadded z)) ) isPadded +unpad = \ n . n 0 ( \ z . ( \ unpadZ . isStrictZero? unpadZ (shiftR0 unpadZ) 0 ) (unpad z) ) (B shiftR1 unpad) +isPadded? = \ n . n False ( \ z . z True (K (isPadded? z)) (K (isPadded? z)) ) isPadded? # instance Ord @@ -99,11 +99,11 @@ bitXor = \ m n . m n ( \ zm . n ( dbl zm) (B dbl (bitXor zm)) (B shiftR1 (bitXor zm)) ) ( \ zm . n (shiftR1 zm) (B shiftR1 (bitXor zm)) (B dbl (bitXor zm)) ) -testBit = \ i n . isZero i +testBit = \ i n . isZero? i (n False (testBit (pred i)) (testBit (pred i))) (n False (K False) (K True)) -bit = \ i . isZero i (shiftR0 (bit (pred i))) (succ i) +bit = \ i . isZero? i (shiftR0 (bit (pred i))) (succ i) popCount = \ n . n 0 popCount (B succ popCount) @@ -139,7 +139,7 @@ minus = \ m n . gt m n 0 (go m n) until = \ p fn x . p x (until p fn (fn x)) x divMod = \ m n . until (B (lt m) snd) (bimap succ shiftR0) (Pair 0 n) - \ steps nn . isZero steps + \ steps nn . isZero? steps (divMod (minus m (shiftL nn)) n (B Pair (plus (bit (pred steps))))) (Pair 0 m) div = \ m n . fst (divMod m n) @@ -160,7 +160,7 @@ gcd = \ m n . m n (gcd (minus m n) n) ) ) -lcm = \ m n . T (gcd m n) \ g . isZero g (times (div m g) n) g +lcm = \ m n . T (gcd m n) \ g . isZero? g (times (div m g) n) g min = \ m n . le m n n m max = \ m n . le m n m n diff --git a/tests/basics-binary-scott/test.js b/tests/basics-binary-scott/test.js index b7b1124..d8ec89d 100644 --- a/tests/basics-binary-scott/test.js +++ b/tests/basics-binary-scott/test.js @@ -11,7 +11,7 @@ const {fromInt,toInt} = LC; const {False,True,not,and,or,xor,implies} = solution; const {LT,EQ,GT,compare,lt,le,eq,ge,gt} = solution; const {Pair,fst,snd,first,second,both,bimap,curry} = solution; -const {shiftR0,shiftR1,shiftL,dbl,isStrictZero,isZero,pad,unpad,isPadded} = solution; +const {shiftR0,shiftR1,shiftL,dbl,isStrictZero,isZero,pad,unpad,"isPadded?":isPadded} = solution; const {succ,pred} = solution; const {bitAnd,bitOr,bitXor,testBit,bit,popCount,even,odd} = solution; const {plus,times,minus,divMod,div,mod,pow,gcd,lcm,min,max} = solution;