Snobol Introduction

by "Blag" - Senior Developer Evangelist

Return to Geeky Thursday

What is Snobol?


StriNg Orinted and SymBOLic Language.


Released on 1967.

Imperative and Unstructured.


Patterns are first-class data types.


It's string manipulation capabilities are were awesome.

How to install Snobol


On Linux...


wget ftp://ftp.ultimate.com/snobol/snobol4-1.5.tar.gz

tar –zxvf snobol4-1.5.tar.gz && cd snobol4-1.5

sudo apt-get install m4

sudo make install

Who uses Snobol?


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

Starting out...


You can simply call it from the terminal

by typing "snobol4"

Comments


Comments are very simple

				
* This is a comment
				

Variables


There's no data type declaration...


The data type changes based on its value


				
    myVar = 'Blag'
    
    output = myVar
    
    myVar = 42
    
    output = myVar
    
end
				

Success and Failure


The way Snobol works is that, somethhi9ng works or it doesn't.

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

				
        num = input
        
        gt(num,5)	    :s(print)f(end)
        
print   output = num

end
				

We ask for a number and print it if bigger than 5...

Otherwise, the program just ends...

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
				

We ask for a number, check that is bigger than zero, print it, decrease it and check again.

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
				

sum is the function name and a and b are the parameters.

sum0 is the function entry point and sum_end is the end of the function.

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
				

The array will have 3 elements, all filled with zero.

We assign the values and them print in a row.

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
				

We create a table and assign two indices with values.

We add the value on one of the indices.

We print the values in a single line.

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
				

"ANY" will match any vowel and we create a loop to replace them by "*"

Th*r* *r* n* v*w*ls h*r*

Fibonacci list sequence


Finally...we're going to make our first application...


Name your file "Fibonacci.sno"


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	
				

When we run it we're going to see...


Making an LCD Number App


This is my favorite application of all time...


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
				

When we run it we're going to see...

Decimal to Romans


This App will create a Roman Numeral based on a decimal number


This will include some nice commands...


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
				

When we run it we're going to see...

Counting Letters


In this example we're going to read a file and count how many time a letter appears...


Call your file "countletters.sno".


Create a file called "readme.txt" with the following text...


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


				
       letters = table()
        
       letter = ''
        
       counter = 0
        
       len = 0
        
       auxline = ''
        
       INPUT('readline',1,,'readme.txt')     :s(read)
        
read   auxline = readline

       len = size(auxline)
        
       auxline len(counter) len(1) . letter
        
       letters<letter> = letters<letter> + 1
        
       counter = counter + 1
        
       eq(counter,len)                       :s(done)f(read)
				
				
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
				

When we run it we're going to see...


That's it for now...


I hope you had some fun...


Snobol is a fascinating language


Maybe not the most versatile, but fascinating nevertheless


Contact Information


Blag --> blag@blagarts.com

@Blag on Twitter

Go back home...