Why learning an old Programming Language is the right thing to do
Isometric Room by Blag (On Unreal Engine)

Why learning an old Programming Language is the right thing to do

People tend to look at old programming languages like useless and obsolete things...while they are the complete opposite. And old programming language can teach you a lot and force to think outside the box.

In one of my articles called "How do I learn programming", I talked about how I always develop 5 examples on every new programming language that I learn. One of those examples is "CountLetters" where I have a text like this...

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

Where "XXX" is the programming language being used...

This is a trivial example if you use a current programming language, for example Crystal (2014).

line = File.read("readme.txt").strip
counter = {} of String => Int32
line.each_char{|x| if counter.has_key?(x.to_s)
                     counter[x.to_s] += 1
                   else
                     counter = counter.merge({x.to_s => 1})
                   end }
counter.each do |key, value|
  puts "#{key} => #{value}"
end
No alt text provided for this image

Or Golang (2009)

package main

import ( "fmt"; "io/ioutil"; "strings" )

func main() {
    file, err := ioutil.ReadFile("readme.txt")
    if err != nil {
        fmt.Print("Couldn't open the file")
        return
    }
	str := string(file)
    str = strings.Replace(str, "\n", "", 1)
    Chars := strings.Split(str, "")
    charmap := make(map[string]int)
    for _, value := range Chars {
        charmap[value] += 1
    }
    for key, value := range charmap {
        fmt.Print("(", key, ", ")
        fmt.Println("\"", value, "\")")
    }
}
No alt text provided for this image

Now...let's say you want to use a never used before programming language, just because you want to learn a new programming paradigm or simply because you want a challenge.

Using an old programming language is indeed challenging...not only because you need to learn the language, or the paradigm, but because old programming languages were limited in the sense that they didn't had many keywords or maybe because arrays didn't even exist at that particular time or because the documentation didn't exactly made it more current times.

To illustrate this, let's see some example using some old or not well know programming languages.

Let's start with Icon (1977). A multi-paradigm, structured and text-oriented programming language.

procedure main()
	local intext, outtext, letters, x, i , j, letter
	letters := table()
	letter := ""
	i := 1
	j := 2
	intext := open("readme.txt") | stop("Cannot open file")
	outtext := read(intext)
	
	every x := 1 to *outtext do{
		letter := outtext[i:j]
		i +:= 1
		j +:= 1
		if \letters[letter] then letters[letter] +:= 1
		else insert(letters,letter,1)
	}
	
	every x := key(letters) do
		write(x," : ",letters[x])
end
No alt text provided for this image

That one wasn't really hard as they didn't had Arrays, but had Tables :)

Let's go a little more back in time...to SmallTalk (1972). A Object-Oriented, dynamically-typed, Reflective programming language.

| file contents letters |

letters := Dictionary new.

file := FileStream open: 'readme.txt' mode: FileStream read.
file linesDo: [ :c | contents := c ].

1 to:contents size do: [:x |
	(letters includesKey:((contents at:x) asString)) ifTrue: [
		letters at:((contents at:x) asString) 
		        put:((letters at:((contents at:x) asString)) + 1)
	] ifFalse: [
		letters at:((contents at:x) asString) put:1.
	].
].

letters keysAndValuesDo: [:aKey :aValue | Transcript
   show: aKey printString; space;
   show: aValue printString; cr
].

file close

This one was a little bit harder but not so much, as again...no Arrays but Dictionaries.

Let's more to a different paradigm altogether...although newer programming language...with Miranda (1985), a Lazy, purely functional programming language.

readFile :: [char]
readFile = read "readme.txt"

getLetter :: ([char], num) -> char
getLetter (list, counter) = list ! counter 

countLetters :: (char,[[char]],num,num,num) -> [char]
countLetters(letter,list,top,counter,count) 
= countLetters(letter,list,top,counter + 1, count + 1), 
  if(letter = ((list ! 0) ! counter) & counter < top)
= countLetters(letter,list,top,counter + 1, count), 
  if(letter ~= ((list ! 0) ! counter) & counter < top)
= (letter : "") ++ " --> " ++ shownum(count), otherwise

print :: ([char]) -> [char]
print(file) = "\n" ++ countLetters(file ! 0,[file],# file - 1,0,0) ++ print(remove(file,[file ! 0],0)), if(# file > 1)
print(file) = "", if(# file <= 0)
            = "", otherwise

main :: [char]
main = print(readFile)

remove :: ([char], [char], num) -> [char]
remove(file,letter,counter) = remove((file -- letter),letter,counter + 1), if(counter < # file - 1)
remove(file,letter,counter) = file, if(counter > # file)
                            = file, otherwise
No alt text provided for this image

Now...this one was really...really hard...I mean...functional programming is not easy to grasp...but besides that...in Miranda, there are not arrays...or tables or dictionaries...heck...there's not even variables! There's only lists...so for this one I really need to think hard and design the app in a completely different way...

Finally, let's a look at Snobol (1962), the older from our list. A text-string oriented language, unstructured and imperative.

* Count Letters

		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
No alt text provided for this image

This one wasn't exactly easy. Snobol is a goal-oriented language, which is not as easy to grasp as it might sound ;)

As a bonus...I couldn't stop myself from including good old Prolog (1972). A logic programming language associated with AI.

main(X):- open('readme.txt',read,Str), 
          read(Str,Line), 
          X = Line,
          close(Str).

count_occurrences(List, Occ):-
    findall([X,L], (bagof(true,member(X,List),Xs), length(Xs,L)), Occ).

count_letters(Y) :- main(X), atom_chars(X,L), count_occurrences(L,Y).

This was kind of tricky...although the source code is small...logic programming can be complex sometimes...

With these examples...can you see what I mean? Forcing yourself to think makes you a better developer...

Blag - Programming Languages Archaeologist.


要查看或添加评论,请登录

Alvaro (Blag) Tejada Galindo ??的更多文章

社区洞察

其他会员也浏览了