Some useful useless stuff

Cléber Zavadniak

2022

Cléber Zavadniak

  • ~1999: "Apostila de C"
  • 2004: Computing Science (incomplete)
  • 2004: C(microcontrollers) / C++
  • Linux+Networks
  • PHP+MySQL
  • 2013: Python[Django]+MySQL+integrations
  • Python[Django]+Postgres+EDA+Microservicos+REST
  • Python[Django]+Postgres+RPC+Mezzoservices+REST
  • Python[Flask]+SQLAlchemy+Postgres+QuasiREST
  • 2020: Go+Postgres+GraphQL
  • 2021: Lucid

 

https://cleber.solutions/

"How do I create a JS function

to be called recursively

that retains state

but without access to global/outer scope?"

% 1- The "interface" function:
fib(N) -> fib_iter(N, 0, 1).

% 2- The break:
fib_iter(0, Result, _Next) -> Result;

% 3- The iteration:
fib_iter(Iter, Result, Next) ->
    fib_iter(Iter-1, Next, Result+Next).

Erlang example

fib(N) -> fib_iter(N, 0, 1).

fib_iter(0, Result, _Next) -> Result;
fib_iter(Iter, Result, Next) ->
    fib_iter(Iter-1, Next, Result+Next).

 

fib(3)             % interface
fib_iter(3, 0, 1)  % iteration
fib_iter(2, 1, 1)  % iteration
fib_iter(1, 1, 2)  % iteration
fib_iter(0, 2, 3)  % break
2                  % result

Index:    0 1 2 3
Sequence: 0 1 1 2

Some honorable mentions

Before we really start,

of (probably) very useful "useless" things.

LISP

Tcl

Haskell

Zig

Now, forget everything.

Prolog

Logical Programming

Logical Programming

  • Based in... Logic.
    • Basically, the structured study of the truth
    • Among other things, "predicate calculus"
  • The core of a Prolog program: find the truth.

You sure you forgot everything?

  • Variables
  • Functions
  • Flow control

 

Forget it.

In the terminal

swipl -f awesome.pl

swipl -f roads.pl
path(a, e, Path, Distance).
path(a, e, Path, Distance), [H|T] = Path, [H2|T2] = T, H2 = b.

 

append([a], [b], X).

append(A, B, [a, b, c, d]).

 

<comma> = AND | <semicolon> = OR/ELSE | <period> = END

See:

https://medium.com/clebertech-en/prolog-is-not-that-hard-f8cb2b8b3e43
https://medium.com/clebertech-en/prolog-is-not-that-hard-part-2-3a88ac8f02e0

Use cases

  • Logic
  • Combinatorial Analisys
  • Artificial Inteligente
  • Compilers (semantic checks)
  • Automatic tests generators

 

Interesting example: "julian"

Date and time, based only on constraints.

julian

:- use_module(library(julian)).
:- use_module(library(clpfd)).

solution(Year) :-
    % Eisenhower presidency had
    % Fourth of July on Sunday in ...
    form_time([dow(sunday), Year-07-04]),
    Year in 1953..1961.

?- solution(Y).
Y = 1954.

In other languages

  • Python
    • kanren ("logpy")
  • Go
    • golog
  • Javascript
    • tau-prolog

Forth

Concatenative languages

"Stack based"

  • Operations always from the stack
  • The stack is pure

 

"concatenative" is not a synonym

with "stack based".

 

Concatenative:

Prog_A   <concat operation>   ProgB =

Prog_B(Prog_A)

Stack

  • []  # Empty
  •  
  • 100 → [100]
  • 200 → [100, 200]
  • 300 → [100, 200, 300]
  •  
  • + → [100, 500]
  • * → [50000]

Code sample

\ Display indexes of a 2D array
: INDEXES  ( rows columns -- )  
   SWAP          ( columns rows )
   0 ?DO         \ For number of rows 
      CR         \ CR goes to next line
      DUP        \ DUPlicate columns
      0 ?DO      \ For number of rows  
          J .    \ Display row index
          I .    \ Display column index
          SPACE  \ SPACE displays a space
     LOOP        \ End of inner loop
  LOOP ;         \ End of outer loop
3 4 INDEXES

But... WHY?

Imagine you have only

32 bytes

of available memory

and have to run some fairly complex algorithms.

Too far fetched?

Argo Submersible Vehicle

(Now retired) Space Shuttle

Forth

  • 2 stacks:
    • Values
    • Return addresses
  • Partially compiled, partially interpreted
    • Threaded code
    • Built-in words are compiled
    • User-defined words are a sequence of jumps between other words

C

  • 1 stack:
    • Activation records
    • Arguments and runtime addresses share the same space
  • Compiled?
    • Yes (C)
    • No (shell)
    • Yes, to VM (Python)
      • +FFI

Le Fin

 

My advice:

Take some time to study some absurd topic.

Copy of Técnicas Alternativas de Programação

By Cléber Zavadniak

Copy of Técnicas Alternativas de Programação

  • 133