Example programs

These are general examples that have been made to touch upon the features of Pip.

Some of these may link to questions on Code Golf Stack Exchange, where you can look at them in more detail.

GCD (Greatest Common Divisor) of two numbers

Wb%:aa::ba

Explanation:

Wb%:a       Assign b%a to b and loop while nonzero:
     a::b    Swap a and b
         a  After the loop, print a

Quine

Each of these approaches requires the code to end with a newline, since the output will have a trailing newline.

" X2RsC34.s" X2RsC34.s  Duplicate the string and replace each space with double quote followed by space
Y"Y yRsRPy"yRsRPy       Repr substitution, as in the standard Python quine
V Y"`V Y`.RPy"          Using eval, with a Pattern to allow a string-like object without double quotes

First a Fibonacci numbers, starting with 1

LaPi::o+:i

Explanation:

             i is initially 0; o is 1
La           Loop (arg) times:
      o+:i    Set o to o + i
   i::        Swap values of i and o
  P           Print new value of i

Arithmetic mean of input

$+g/#g

Explanation:

  g      List of cmdline args
   /#g   Divide each element of the list by the length of the list
$+       Sum the result (fold on addition)

Factorial

Three different approaches:

a?a*REa-1o   Recursive...
Fi\,ao*:io   Iterative...
$*\,a        But fold is the best!

FizzBuzz

LhP J["Fizz""Buzz"]X!*Ui%^35|i

Explanation:

Lh                              Loop 100 times:
  P                             Print this expression:
                         ^35    35, split into a list of characters [3;5]
                      Ui%       Increment i and take its new value mod each of those numbers
                    !*          Logically negate each value (0 -> 1, nonzero -> 0)
     ["Fizz""Buzz"]X            String-multiply "Fizz" and "Buzz", itemwise, by the above
                                The result is a list containing "Fizz" or "" depending on i%3
                                and "Buzz" or "" depending on i%5
    J                           Join that list into a string
                            |i  Logical or with i (i.e. use the number if the resulting string is "")

Translate alphanumeric phone numbers

{aQ'z?9aNz?5*Aa//16-28a}Ma

Is a number divisible by all of its digits?

0=$+a%^a

Explanation:

      ^a  Split num (as string) into an array of its digits                              [1;2;8]  [2;0]   [3;2]
    a%    Take num mod each of those digits; if a digit is zero, the result will be nil  [0;0;0]  [0;()]  [2;0]
  $+      Sum the resulting list (note: summing a list containing nil results in nil!)   0        ()      2
0=        Iff the sum equals 0, return 1 (true); otherwise (>0 or nil), return 0 (false) 1        0       0

Build nested lists

a?Da?[0UREa][0]l   16 bytes (uses -p flag)

Explanation:

a?             l   If input is 0, return empty list
  Da?       [0]    Else, decrement input; if it is now 0, return [0]
        REa        Else, recurse using the decremented value...
       U           ... and increment the result itemwise...
     [0    ]       ... and make it the second item in a new outer list
                   The recursive main function thus builds up lists like [0] -> [0;[1]] -> [0;[1;[2]]] etc.

Copyright © 2015-2024 David Loscutoff. Distributed on Github.