# define primrec in joy

## icpdesign — 2005-08-20 21:24:49

    The paper 'An Informal Tutorial on Joy', by Manfred von Thun, introduces the combinator

    primrec to compute the factorial of any integer. I am wondering if it is possible to define

    primrec using joy:

    DEFINE

       primrec == ...

    Thanks

    Taoufik

## icpdesign — 2005-08-20 21:33:07

    sorry but I am afraid that my previous message may not be clear.

    The paper 'An Informal Tutorial on Joy', by Manfred von Thun, introduces the

    combinator primrec that may be used to compute the factorial of any integer as follows:

    factorial == [1] [*] primrec

    5 fatorial -> 120

    I am wondering if it is possible to define primrec using joy language:

    DEFINE

       primrec == ...

    Thanks

    Taoufik

## Ivan Tomac — 2005-08-21 04:04:09

    --- In concatenative@yahoogroups.com, "icpdesign"

    <taoufik.dachraoui@w...> wrote:

     > The paper 'An Informal Tutorial on Joy', by Manfred von Thun,

     introduces the combinator

     > primrec to compute the factorial of any integer. I am wondering if

     it is possible to define

     > primrec using joy:

    >

    > DEFINE

    >    primrec == ...

    >

     I don't have time to look into it right now but yes, it is possible.

    Have a look at Brent Kirby's paper on Joy, "The Theory of

    Concatenative Combinators" (http://tunes.org/~iepos/joy.html). It has

    a lot of examples of how to construct combinators using other

    combinators. It also mentions a few minimal bases of just two

    combinators that can be used to create all other combinators.

     > Thanks

    > Taoufik

     Ivan

## phimvt@lurac.latrobe.edu.au — 2005-08-22 07:40:53

    On Sat, 20 Aug 2005, icpdesign wrote:

    [..]

    > I am wondering if it is possible to define primrec using joy

    language:

    >

    > DEFINE

    >    primrec == ...

    Here is a recursive solution:

    DEFINE

      pr ==

        [ pop pop small ]

        [ pop pop ]

        [ [dup pred] dipd

          dup

          [pr] dip                   # recursion here

          i ]

        ifte;

      primrec ==

        [first] dip pr.

    Note that pr could be used like this:

      5 1 [*] pr    ==>  120

    Indeed, one might argue that pr is "better" than primrec.

    If you did not want the recursion, there is likely to be another,

    nonrecursive solution using the repeat combinator. But, as they say, "this

    is left as an exercise".

      - Manfred