# Snobol Introduction

## Who uses Snobol?

• AT&T Bell (It was creaated at Bell Labs)
• AWK and Perl made Snobol a bit outdated..

## Starting out...

### Comments are very simple

```				```
* This is a comment
``````

## Variables

### The data type changes based on its value

```				```
myVar = 'Blag'

output = myVar

myVar = 42

output = myVar

end
``````

## Success and Failure

### Using "Goto's", we can control the flow of the program.

```				```
num = input

gt(num,5)	    :s(print)f(end)

print   output = num

end
``````

## Loops

### There are no loops...we can easily build them...

```				```
num = input

output = ''     :s(check)

check   gt(num,0)       :s(loop)f(end)

loop    output = num

num = num - 1   :s(check)

end
``````

## Functions

### Function are very important...that's true for every programming language...

```				```
define('sum(a,b)','sum0')       :(sum_end)

sum0      sum = a + b               :(return)

sum_end

output = sum(2,30)

end
``````

## Arrays

### Arrays are easy

```				```
array = array(3,0)

array<1> = 1

array<2> = 2

array<3> = 3

output = array<1> ' ' array<2> ' ' array<3>

end
``````

## Tables

### Tables allow us to define our own indices

```				```
tab = table()

tab<'a'> = 1

tab<'b'> = 1

tab<'a'> = tab<'a'> + 1

output = tab<'a'> ' ' tab<'b'>

end
``````

## Pattern Matching

### One of Snobol's biggest strenghts, it's Pattern Matching.

```				```
text = 'Java is awesome. Snobol not so much'

text 'Snobol' = 'Java'

text 'Java' = 'Snobol'

output = text

end
``````

### Snobol is awesome. Java not so much

```				```
text = 'There are no vowels here'

vowel = any('aeiou')

again	text vowel = '*'           :s(again)f(print)

print	output = text

end
``````

## Fibonacci list sequence

### we can run it by calling snobol4 nameoffile.sno

```				```
define('fibo(num,a,b)temp','fib')    :(fibo_end)

fib     fibo = eq(a,0)  a ' ' b ' ' ( a + b )

num = num - 1

a = a + b                            :s(fib0)

fib0    gt(num,1)                            :s(fib1)f(return)

fib1    fibo = gt(a,0) fibo ' ' ( a + b )

num = num - 1

temp = a

a = a + b

b = temp                             :s(fib0)f(return)

fibo_end
``````
```				```
output = 'Enter a number: '

num = input

output = fibo(num,0,1)

end
``````

## Making an LCD Number App

### Name your file "LED_Numbers.sno"

```				```
led = array(10, '')

led<1> = ' _  | | |_| '

led<2> = '     |   |  '

led<3> = ' _   _| |_  '

led<4> = '_   _|  _|  '

led<5> = '    |_|   | '

led<6> = ' _  |_   _| '

led<7> = ' _  |_  |_| '

led<8> = '_    |   |  '

led<9> = ' _  |_| |_| '

led<10> = ' _  |_|  _| '
``````
```				```
output = "Enter a number: "

num = input

i = 0

digits  num len(i) len(1) . digit          :f(done)

digit = digit + 1

led<digit> len(0) len(4) . line_aux

line1 = line1 line_aux

led<digit> len(4) len(4) . line_aux

line2 = line2 line_aux

led<digit> len(8) len(4) . line_aux

line3 = line3 line_aux

i = i + 1                          :s(digits)
``````
```				```
done    output = line1

output = line2

output = line3

output = ''

end
``````

## Decimal to Romans

### Name your file "Decimal_to_Roman.sno"

```				```
roman_table = table()

roman_table<1000> = 'M'

roman_table<900> = 'CM'

roman_table<500> = 'D'

roman_table<400> = 'CD'

roman_table<100> = 'C'

roman_table<90> = 'XC'

roman_table<50> = 'L'

roman_table<40> = 'XL'

roman_table<10> = 'X'

roman_table<9> = 'IX'

roman_table<5> = 'V'

roman_table<4> = 'IV'

roman_table<1> = 'I'
``````
```				```
roman_keys = array(13,1)

roman_keys<1> = 1000

roman_keys<2> = 900

roman_keys<3> = 500

roman_keys<4> = 400

roman_keys<5> = 100

roman_keys<6> = 90

roman_keys<7> = 50

roman_keys<8> = 40

roman_keys<9> = 10

roman_keys<10> = 9

roman_keys<11> = 5

roman_keys<12> = 4

roman_keys<13> = 1

``````
```				```
output = "Enter a number: "

num = input

counter = 1

key_num = 0

check   gt(num,0)                                                   :s(romans)f(done)

romans  key_num = roman_keys<counter>

roman_num = ge(num,key_num) roman_num roman_table<key_num>  :s(dec)f(count)

dec     num = num - key_num                                         :s(check)

count   counter = counter + 1                                       :(check)

done    output = roman_num

output = ''

end
``````

## Counting Letters

### "This is a text file that we're going to read using Snobol"

```				```
letters = table()

letter = ''

counter = 0

len = 0

auxline = ''

len = size(auxline)

auxline len(counter) len(1) . letter

letters<letter> = letters<letter> + 1

counter = counter + 1

``````
```				```
done   aletters = convert(letters,'ARRAY')         :f(empty)

i = 0

print  i = i + 1

output = aletters<i,1> '-->' aletters<i,2>  :s(print)f(end)

empty

end
``````