How to resolve the algorithm Church numerals step by step in the Quackery programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Church numerals step by step in the Quackery programming language

Table of Contents

Problem Statement

In the Church encoding of natural numbers, the number N is encoded by a function that applies its first argument N times to its second argument.

Arithmetic operations on natural numbers can be similarly represented as functions on Church numerals. In your language define:

You should:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Church numerals step by step in the Quackery programming language

Source code in the quackery programming language

  [ this nested ]           is zero  (       --> cn )

  [ this nested join ]      is succ  (    cn --> cn )

  [ zero
    [ 2dup = if done
      succ
      rot succ unrot
      recurse ]
    2drop ]                 is add   ( cn cn --> cn )

  [ zero unrot zero
    [ 2dup = if done
      succ
      2swap
      tuck add swap
      2swap recurse ]
    2drop drop ]            is mul   ( cn cn --> cn )

  [ zero succ unrot zero
    [ 2dup = if done
      succ
      2swap
      tuck mul swap
      2swap recurse ]
    2drop drop ]            is exp   ( cn cn --> cn )

  [ zero swap times succ ]  is n->cn (     n --> cn )

  [ size 1 - ]              is cn->n (    cn -->  n )

  ( - - - - - - - - - - - - - - - - - - - - - - - - )

  [ zero succ succ succ ]   is three (       --> cn )
 
  [ three succ ]            is four  (       --> cn )
 
  four three add cn->n echo sp
  four three mul cn->n echo sp
  four three exp cn->n echo sp
  three four exp cn->n echo

  

You may also check:How to resolve the algorithm Maze generation step by step in the Tcl programming language
You may also check:How to resolve the algorithm Check input device is a terminal step by step in the Quackery programming language
You may also check:How to resolve the algorithm Character codes step by step in the Maple programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Naming conventions step by step in the J programming language