-
Notifications
You must be signed in to change notification settings - Fork 5
1. Getting started
Welcome to Pip! Let's start with some basics.
The easiest way to run Pip code is at Attempt This Online. You'll see several sections on that page; for now, the important ones are Code (where you'll enter your code) and Arguments (where you'll enter your arguments). Click the Execute button to run your program. The output will appear in the stdout box, and any error messages will appear in the stderr box.
Every combination of code and inputs on ATO generates a unique permalink in the URL bar. Most of the examples in this tutorial come with ATO links. You can visit those links and play around with the code yourself to improve your understanding of Pip.
Note: The arguments on ATO have to be formatted as a JSON (or Python) style list. For example, to pass the two arguments
abc
and123
, you'd enter["abc", 123]
in the Arguments box. This format is not the same list format that Pip uses, unfortunately, but it's the same interface for all languages on ATO.
Let's start with a classic:
"Hello, World!"
As you've guessed, this program outputs Hello, World!
. (ATO)
Pip gets a lot of mileage out of expressions. Many programs, like this one, consist of a single expression (in this case, a string literal). If a Pip program ends with an expression, that expression is automatically printed with a trailing newline.
More-complex expressions are constructed using infix operators. Here's a program that adds two numbers:
23+19
The result, 42
, is calculated and printed with a trailing newline. (ATO)
Operators have precedence, just like in traditional programming languages. For example, multiplication has higher precedence than subtraction:
20-6*3
outputs 2
, not 42
. (ATO)
Parentheses force a subexpression to be calculated first:
(20-6)*3
now gives 42
. (ATO)
Operators are used for more than just arithmetic. In fact, most things you'll want to do in Pip involve operators. For example, the unary #
operator gives the length of a string, and the binary .
operator does string concatenation:
#"Hello, World!"." characters"
Since #
has higher precedence than .
, the length operation is applied before the concatenation, so this program outputs 13 characters
. (ATO)
When your program needs to take input, the usual way to do it is via command-line arguments. The first command-line argument can be referenced in your code as a
. For example, here's a program that takes a number, multiplies it by 3, and adds 1:
a*3+1
Notice how the input number is specified in the Arguments box: ATO
We'll talk about other ways of taking input later.
Believe it or not, we now know enough to solve an actual code-golf challenge! It's a simple one, but it demonstrates everything we've covered so far: the 2spooky4me challenge.
Given a positive integer n, output a string based on the template (n)spooky(n+2)me
. For example:
Input | Output |
---|---|
1 | 1spooky3me |
2 | 2spooky4me |
3 | 3spooky5me |
8 | 8spooky10me |
29 | 29spooky31me |
We already know how to take an input and do arithmetic with it. A program that adds 2 to the input is just a+2
. We'll also need to concatenate the numbers to the literal strings spooky
and me
. Putting it all together:
a."spooky".(a+2)."me"
As it turns out, arithmetic operators have higher precedence than string concatenation, so we don't need the parentheses:
a."spooky".a+2."me"
(ATO)
At 19 bytes, the above is not the shortest possible solution to this challenge; I've found two different 18-byte solutions. However, those techniques will have to wait for a future chapter.
Written by @dloscutoff and @razetime. Do show us your Pip programs at Stack Exchange Chat!