Dip Programming Language: Examples

# Examples of Dip Code

The following pages demonstrate a few Dip programs for users to get a better idea of Dip's syntax and its use cases. You can run these online, or on your Dip installation. Dip is unfinished - and is aimed at beginners, so it lacks a few features you would normally find useful. However, any suggestions for the improvement of the language are welcome

# Factorial function

``````function factorial(term)variable fact = 1
if term >= 1 then
for i = 1 to term + 1 then
variable fact = fact * i
end
end
print(fact)
end
``````

# Fibonnaci Function

``````function fib(nterms)variable n1 = 0
variable n2 = 1
variable count = 0

if nterms <= 0 then print("Input a positive integer")
if nterms == 1 then
print("Fibonnaci sequence upto" + " 1" + ":")
print(n1)
else
print("Fibonacci sequence:")
while count < nterms then
print(n1)
variable nth = n1 + n2
variable n1 = n2
variable n2 = nth
variable count = count + 1
end
end
end
``````

# Hello World in Dip

``````print("Hello, world!")
``````

# Random

``````
# A function to select a random value from a list
function random(list_)
variable random_number = random_int(0, length(list_) - 1)
return (list_ / random_number)
end
```
```

# Running Python Code

Dip can also run Python programs - using the 'evaluate' function. Through this, users can get the power of Python, using a much easier syntax. The below program shows an implementation of a calculator in Pythin. Try this in your local Dip environment.
``````variable python_code = "
# Program make a simple calculator

# This function adds two numbers
return x + y

# This function subtracts two numbers
def subtract(x, y):
return x - y

# This function multiplies two numbers
def multiply(x, y):
return x * y

# This function divides two numbers
def divide(x, y):
return x / y

print('Select operation.')
print('2.Subtract')
print('3.Multiply')
print('4.Divide')

while True:
# Take input from the user
choice = input('Enter choice(1/2/3/4): ')

# Check if choice is one of the four options
if choice in ('1', '2', '3', '4'):
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))

if choice == '1':
print(num1, '+', num2, '=', add(num1, num2))

elif choice == '2':
print(num1, '-', num2, '=', subtract(num1, num2))

elif choice == '3':
print(num1, '*', num2, '=', multiply(num1, num2))

elif choice == '4':
print(num1, '/', num2, '=', divide(num1, num2))
break
else:
print('Invalid Input')
"

evaluate(python_code)
``````

# Math Functions

```  ```
# Squares a value
function square(num) -> num ^ 2

# Checks if a number is even or odd
function odd_or_even(number)
if number % 2 == 0 then print("even") else print("odd")
end
```
```

# GUI programs in Dip (With Python)

``````variable name = input("What's your name? ")

variable python_code = "import tkinter as tk;" + "root = tk.Tk();" + "root.title = 'Dip GUI';" + "label = tk.Label(root, text='Hello, " + name + "').pack()"

evaluate(python_code)
``````

# Caesar Cipher in Dip

``````# Implementing the Caesar cipher in Dip

# Including all of the letters in the English alphabet

variable alphabet = "abcdefghijklmnopqrstuvwxyz "

# Getting the user's input

variable plaintext = input("Which word / phrase would you like to encrypt: ")

# The key, which adds the value of the key to

variable key = input_integer("What would you like the key to be? ")

variable ciphertext = ""

for character = 0 to length(plaintext) then

for letter = 0 to length(alphabet) then
if plaintext / character == alphabet / letter then
if plaintext / character != " " then
variable index = letter + key
variable ciphertext = ciphertext + alphabet / (index % 26)
else
variable ciphertext = ciphertext + " "
end
end
end
end

print("ciphertext: " + ciphertext)
``````