01Your first program
Every INK program is a file ending in .in, such as hello.in. Here is the traditional first program:
show "Jai Hind!"
Run it with the ink.sh script:
./ink.sh hello.in
Jai Hind!
show is how INK puts something on the screen. It works with both numbers and text.
02Variables and values
A variable stores a value. Create one with let:
let name = "George"
let age = 30
show name
show age
George 30
INK has two basic kinds of value:
num: a whole number, like30,0or-5.text: words in double quotes, like"Hyderabad".
Constants
Use const for values that never change:
const MAX = 100
show MAX
03Maths and joining text
INK does arithmetic with the usual symbols:
let a = 10
let b = 3
show a + b
show a - b
show a * b
show a / b
13 7 30 3
Joining text
The + symbol also joins text. If you add a number to text, INK turns the number into text for you:
let name = "George"
show "Namaste " + name
let age = 30
show "Age: " + age
Namaste George Age: 30
04Making decisions
Use if to do something only when a condition is true:
let age = 25
if age > 18 {
show "Adult"
} else {
show "Young"
}
Adult
Many conditions with else if
let marks = 75
if marks > 90 {
show "Grade A"
} else if marks > 60 {
show "Grade B"
} else {
show "Fail"
}
Grade B
Comparison symbols: > greater, < less, >= greater or equal, <= less or equal, == equal, != not equal.
05Repeating with loops
for: count through a range
for i in 0..5 {
show i
}
0 1 2 3 4
The range 0..5 goes up to, but not including, 5.
while: repeat while something is true
let i = 0
while i < 3 {
show i
i = i + 1
}
0 1 2
break and continue
break stops a loop early. continue skips to the next turn. loop repeats forever until you break.
for i in 0..10 {
if i > 4 {
break
}
show i
}
0 1 2 3 4
06Functions
A function is a reusable piece of code. Define it with fn:
fn add(a: num, b: num) -> num {
return a + b
}
let result = add(5, 3)
show result
8
add. It takes two num values, a and b, and gives back (->) an num. Inside, return sends the answer back.Functions that return text
fn greet(name: text) -> text {
return "Namaste " + name
}
show greet("George")
Namaste George
Recursion: a function calling itself
fn fact(n: num) -> num {
if n < 2 {
return 1
}
return n * fact(n - 1)
}
show fact(5)
120
07Lists
A list holds many values in order. Create one with square brackets:
let scores = [10, 20, 30]
show scores[0]
show scores[1]
show len(scores)
10 20 3
Positions start at 0, so scores[0] is the first item. len gives the number of items.
Changing an item
let nums = [1, 2, 3]
nums[0] = 100
show nums[0]
100
Lists of text
let cities = ["Delhi", "Mumbai", "Hyderabad"]
show cities[2]
Hyderabad
Walking through a list
let nums = [5, 10, 15]
let i = 0
while i < len(nums) {
show nums[i]
i = i + 1
}
5 10 15
08Maps
A map stores values you look up by name (a key) instead of by position. Use curly braces:
let age = {"George": 30, "Priya": 25}
show age["George"]
show age["Priya"]
30 25
Maps with text values
let city = {"George": "Hyderabad", "Priya": "Delhi"}
show city["George"]
show "Priya lives in " + city["Priya"]
Hyderabad Priya lives in Delhi
Maps are perfect for real-world data: a person's details, settings, a phone book or a menu with prices.
09Reading input
INK programs can ask the user for input. input() reads a number:
show "Enter a number:"
let n = input()
show "Doubled:"
show n + n
input_text() reads a line of text:
show "What is your name?"
let name = input_text()
show "Namaste " + name
10Saving and reading files
Programs can save data to a file and read it back, so they remember things between runs.
let msg = "Jai Hind!"
write_file("greeting.txt", msg)
let content = read_file("greeting.txt")
show content
Jai Hind!
write_file(name, text) saves text to a file. read_file(name) reads it back as text (up to 64 KB).
11Built-in helpers
INK comes with ready-made tools, so you don't have to write them yourself.
| Helper | Use | What it does |
|---|---|---|
| len | len(list) | How many items are in a list |
| sum | sum(list) | Adds up all numbers in a list |
| min | min(a, b) | The smaller of two numbers |
| max | max(a, b) | The larger of two numbers |
| abs | abs(x) | The positive version of a number |
| input | input() | Reads a number the user types |
| input_text | input_text() | Reads a line of text the user types |
| write_file | write_file(name, text) | Saves text into a file |
| read_file | read_file(name) | Reads text out of a file |
let nums = [3, 9, 2, 15, 7]
show sum(nums)
show max(nums[0], nums[3])
show abs(0 - 42)
36 15 42
12Safety: how INK protects you
Memory safety
If you ask for a list position that doesn't exist, INK stops with a clear error instead of handing back garbage:
let a = [10, 20, 30]
show a[99]
List index out of range
Type checking
INK checks types before your program runs. Each of these is caught at compile time:
| Code | Error |
|---|---|
| add("hello", 5) | Wrong argument type |
| "text" - 5 | Cannot subtract from text |
| foo(1) | Unknown function |
13Words reference
Every INK word, what C and Python call it, and why INK's choice reads clearly.
| INK | C / Python | Meaning |
|---|---|---|
| show | Show something on screen | |
| let | var / int x = | Create a variable |
| const | const / final | A value that never changes |
| num | int | A whole number |
| text | str / char* | Words and letters |
| list | array / [] | A collection of things, in order |
| fn | def / func | Define a function |
| return | return | Send an answer back from a function |
| -> | -> / : | "Gives back": the type a function returns |
| if / else | if / else / elif | Make a decision |
| for … in | for | Count through a range |
| while | while | Repeat while something is true |
| loop | while (true) | Repeat forever |
| break | break | Stop the loop |
| continue | continue | Skip to the next turn |
| true / false | True / False | Yes and no |
| [ ] | [ ] | Make a list, or pick a position in one |
| { } | dict | Make a map: look up a value by name |
| .. | range() | A range of numbers, as in 0..5 |
| : | : | "Is of type", as in age: num |
At a glance
let x = 5
const MAX = 100
show x
if x > 0 { }
for i in 0..10 { }
while x < 10 { }
fn add(a: num) -> num { }
let a = [1, 2, 3]
let m = {"k": 1}
input() input_text()
read_file write_file
You know INK now.
Try writing a small program of your own, like "ask for 5 numbers and show the biggest". Then share it with us.