How do I learn programming
Study Room - by Blag (Made with Blender 2.79)

How do I learn programming

As most people know...I started learning programming 20 years ago...and the way I learn new programming languages had varied in these long years...

At first, of course I learned by getting books, tutorials and by reviewing source codes...well...that remains the same...but there's something that changed and that had helped me a lot...

Usually, you start with the most obvious "Hello World"...which is of course pretty basic...so when I started working for the d-shop (SAP's Maker Space)...I created a series of classes called "Geeky Thursday" where I could learn new programming languages and then teach them out using a simple format.

I came to the conclusion that to learn a new programming language, the best way is to rebuild some examples build on other previous known languages...and that's how I came with my five favorite "Hello World" examples -;)

For sure, keep in mind that this examples might not be written in the most optimal way or using the most of the command of each programming language...but when you're still learning something new...there's no way you can do that, right? Also...if you're just learning, you want to build something that works by just using the most simple commands, so the new language doesn't really scare you away...

The first is "Fibonacci Sequence" which basically it's just a list of the Fibonacci numbers, so 5 would be 0 1 1 2 3 5 and 10 would be 0 1 1 2 3 5 8 13 21 34 55

"Fibonacci Sequence" - Racket (formerly PLT Scheme of the List-Scheme family)

#lang racket

(define (showFib num)

 (fib num 0 1))



(define (fib num a b)

 ( cond [(and (> a 0) (> num 1)) 

     (append (cons (+ a b) '()) 

         (fib (sub1 num) (+ a b) a))]

     [(= a 0) 

     (append (append (cons a (cons b '())) 

             (cons (+ a b) '())) 

         (fib(sub1 num)(+ a b) b))]

     [(= 1 num) (append '())]))

 
  

"Fibonacci Sequence" - Mercury (Functional Logic Programming Language)

:- module fibonacci.
:- interface.
:- import_module io.

:- pred main(io::di, io::uo) is det.

:- implementation.
:- import_module string, int.

:- pred fibo(int::in, int::in, int::in, string::out) is det.

fibo(Num, A, B, Fibs) :-
	(	
		if A = 0 
		then fibo(Num-1,A+B,B,Fib), Fibs = int_to_string(A) ++ " " ++ int_to_string(B) ++ 
		                                   " " ++ int_to_string(A+B) ++ " " ++ Fib
		else if A > 0, Num > 1
		then fibo(Num-1,A+B,A,Fib), Fibs = int_to_string(A+B) ++ " " ++ Fib
		else Fibs = ""
	).
	
main(!IO) :-
	io.write_string("Enter a number: ",!IO),
	io.read_line_as_string(Result, !IO),
	(	if
			Result = ok(String),
			string.to_int(string.strip(String), N)
		then
			fibo(N,0,1,Fibs),
			io.write_string(Fibs,!IO)
		else
			io.write_string("Not a number...",!IO)
	).
 
  

The second on is "LED Numbers" and this one has actually become my complete favorite example of all time...this is the only one that I have actually code in 34 languages so far (with one more coming...but Factor is not being so nice to me...I was able to code of course but not put it into words because of the Stack Effects...but that's something else...)

Anyway...in this example basically you grab a number like 1977 and print like this...

I love it because it uses many commands that are way different in many programming languages and it's not always that easy to implement...

"LED Numbers" - Crystal (Fast a C, slick as Ruby)

leds = {0 => [" _  ","| | ","|_| "],
        1 => ["  ","| ","| "],
        2 => [" _  "," _| ","|_  "],
        3 => ["_  ","_| ","_| "],
        4 => ["    ","|_| ","  | "],
        5 => [" _  ","|_  "," _| "],
        6 => [" _  ","|_  ","|_| "],
        7 => ["_   "," |  "," |  "],
        8 => [" _  ","|_| ","|_| "],
        9 => [" _  ","|_| "," _| "]}

print "Enter a number: "
num = gets.as(String).strip

i = 0
loop do
  j = 0
  loop do
    print leds[num[j].to_i][i]
    break if j == num.size - 1
    j += 1
  end
  puts ""
  break if i == 2
  i += 1
end

 
  

"LED Numbers" - Prolog (Logic Programming Language, associated with AI and Computational Linguistics)

number(0,[[' _  '],['| | '],['|_| ']]).
number(1,[['  '],['| '],['| ']]).
number(2,[[' _  '],[' _| '],['|_  ']]).
number(3,[['_  '],['_| '],['_| ']]).
number(4,[['    '],['|_| '],['  | ']]).
number(5,[[' _  '],['|_  '],[' _| ']]).
number(6,[[' _  '],['|_  '],['|_| ']]).
number(7,[['_   '],[' |  '],[' |  ']]).
number(8,[[' _  '],['|_| '],['|_| ']]).
number(9,[[' _  '],['|_| '],[' _| ']]).

digits(0,[]).
digits(X,[H|T]) :- (X/10 > 0 -> H1 is floor(X/10), H is X mod 10, digits(H1,T)), !.

accRev([],A,A).
accRev([H|T],A,R) :- accRev(T,[H|A],R). 

getDigits(L,R) :- digits(L,Y), accRev(Y, [], R).

show_records([]).
show_records([A|B]) :-
  print_records(A), nl,
  show_records(B).  

print_records([]).
print_records([A|B]) :-
  format('~w',A), 
  print_records(B).

merge([L], L).
merge([H1,H2|T], R) :- maplist(append, H1, H2, H),
    merge([H|T], R), !.

listnum([],[]).
listnum([H1|T1],[R|Y]) :- number(H1,R), listnum(T1,Y).

led(X) :- getDigits(X,Y), listnum(Y,Z), merge(Z,R), show_records(R).
 
  

The third one is "Decimal to Romans"...something lighter were you simply input let's say 2018 and you get back MMXVIII

"Decimal to Romans" - Dao (Lightweight and optionally type Programming Language)

invar Roman_Table = {1000 => 'M', 900 => 'CM', 500 => 'D', 
                     400 => 'CD', 100 => 'C', 90 => 'XC', 
                     50 => 'L', 40 => 'XL', 10 => 'X', 
                     9 => 'IX', 5 => 'V', 4 => 'IV', 1 => 'I'}

routine roman_number(number :int){
  var result = ""
  while(number > 0){
    var counter = 0
    Roman_Table.keys().sort($descend).iterate(){[key]
      if(number >= key and counter == 0){
        result += Roman_Table[key]
        number -= key
        counter += 1
      }
    }
  }
  return result
}

io.write('Enter a number: ')

invar num = io.read()

io.writeln(roman_number((int)num))
 
  

"Decimal to Romans" - Haskell (Purely Functional Programming Language)

showRomans :: Int -> IO()
showRomans(num) = do
	putStr $ concat $ get_roman num 0

get_roman :: Int -> Int -> [[Char]]
get_roman num ctr
	| num >= roman = [snd $ roman_nums !! ctr] ++ get_roman(num - roman) ctr
	| num < roman && num > 0 = get_roman(num) (ctr+1)
	| num <= 0 = ["\n"]
	where roman = fst $ roman_nums !! ctr
	      roman_nums = [(1000,"M"),(900,"CM"),(500,"D"),(400,"CD"),
						(100,"C"),(90,"XC"),(50,"L"),(40,"XL"),
						(10,"X"),(9,"IX"),(5,"V"),(4,"IV"),(1,"I")]

 
  

The fourth one is "Random Names"...which is something more to test the execution speed...basically you have two arrays of 16 elements...one with names and one with last names...and the idea is to have a new array with 100, 000 elements which are random combinations of names and last names...

"Random Names" - Julia (High-level, high-performance dynamic programming language for numerical computing)

start = time()

names=["Anne","Gigi","Blag","Juergen","Marek","Ingo","Lars",
       "Julia", "Danielle","Rocky","Julien","Uwe","Myles",
       "Mike","Steven", "Fanny"]

last_names=["Hardy","Read","Tejada","Schmerder","Kowalkiewicz",       
            "Sauerzapf","Karg","Satsuta","Keene","Ongkowidjojo",
            "Vayssiere","Kylau","Fenlon","Flynn","Taylor","Tan"]            
            
full_names=AbstractString[]

full_name = ""
		
for i = 1:100000
  full_name = names[rand(1:16)] * " " * last_names[rand(1:16)]
  push!(full_names,full_name)
end

finish = time()

println("Time: ", finish-start)
println(length(full_names), " names generated")
 
  

"Random Names" - Nim (Systems and applications Programming Language)

import random, times

let start = epochTime()

randomize()

let names = ["Anne","Gigi","Blag","Juergen","Marek","Ingo",
             "Lars","Julia", "Danielle","Rocky","Julien",
             "Uwe","Myles","Mike","Steven","Fanny"]

let last_names = ["Hardy","Read","Tejada","Schmerder",
                  "Kowalkiewicz","Sauerzapf","Karg",
                  "Satsuta","Keene","Ongkowidjojo",
                  "Vayssiere","Kylau","Fenlon",
                  "Flynn","Taylor","Tan"]

var full_names: array[0..99999, string]

for i in countup(0, 99999):
  full_names[i] = names[random(16)] & " " & last_names[random(16)]

let finish = epochTime() - start
echo(finish)
 
  

The last one and fifth one is "Count Letters" where we simply read a text file that has the following content...

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

Where XXX - name of the programming language to use...

"Count Letters" - OCaml (Industrial strength programming language supporting functional, imperative and object-oriented styles)

open Core.Std
		
let rec update_list lst_count lst_file i top = 
	if i < top then
		let file_value = List.nth_exn lst_file i in 
		let value = try List.Assoc.find_exn lst_count (file_value) + 1 with Not_found -> 1 in
		let lst_count = List.Assoc.add lst_count (file_value) (value) in
		update_list lst_count lst_file (i+1) top
	else
		for i = 0 to List.length lst_count - 1 do
			let lst_value = List.nth_exn lst_count i in
			printf "%c-->%d\n" (fst lst_value) (snd lst_value)
		done

let () =
	let file = input_line ( open_in "readme.txt") in
	let lst_file = String.to_list file in
	let lst_count = [] in
	update_list lst_count lst_file 0 (List.length lst_file - 1)
 
  

"Count Letters" - Rust (Systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety)

use std::fs::File;
use std::io::BufReader;
use std::io::BufRead;
use std::collections::BTreeMap;

fn letters<'a>(line:&'a str,mut leds:BTreeMap<String, isize>){
	let letters = line.split("");
	let v: Vec<&str> = letters.collect();
	for i in v{
		*(leds.entry(i.to_string()).or_insert(0)) += 1;
	}
	for (key, value) in leds.iter() {
        println!("{} {}", key, value);
    }
}

fn main(){
	let leds: BTreeMap<String, isize> = BTreeMap::new();
	let f = File::open("readme.txt").unwrap();
	let f = BufReader::new(f);
	let mut lines = String::new();
	for line in f.lines(){
		lines = line.unwrap();
	}
	letters(&mut lines, leds);
}
 
  

Now, you can be totally and complete sure that whenever I announce on my Twitter account Blag that I'm learning a new programming language...I'm going to rebuild all these five examples...or at least try...some languages are trickier that other ones and some lack certain functionality...as believe me...I go the extra length to make sure they are almost impossible to build...

You may wonder what I'm learning right now, huh? Well...I learning Factor...and this is the first code I was able to pull out (as I always try to go in the right order)...

"Fibonacci Sequence" - Factor (A Practical Stack Language)

! Copyright (C) 2018 Blag.
! See http://factorcode.org/license.txt for BSD license.
USING: math prettyprint kernel io math.parser command-line namespaces sequences ;
IN: fibo

<PRIVATE

: ask-number ( -- ) "Enter a number: " print ;

: read-number ( -- n ) readln string>number ;

: list_fibo ( x -- )
 1 0
 pick 1 + [ dup . over over + rot drop ] times 3drop ;

PRIVATE>

: fibo ( -- ) ask-number read-number list_fibo ;

: fibo-run ( -- ) fibo ;

MAIN: fibo-run
 
  

And here's "LED Numbers" which I managed to make it work...but not using Words...so I cannot run it like the "Fibonacci Sequence" yet so the code will most likely change a lot...so I'm not putting it here...

Anyway...hope you liked this post -:) I hope you found some interesting programming languages that you might like to try and I hope also that my "5 Hello World" programs help you in your own personal quests of new Programming Languages -;)

Greetings,

Blag. aka "Alvaro Tejada Galindo"

Developer Evangelist - SAP's d-shop

SAP Labs Network

@blag

SAP Labs Toronto.



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

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

社区洞察

其他会员也浏览了