OCaml Programming Examples: Functions and Data Structures

OCaml Programming Examples

Basic Functions

Fibonacci:

let rec fib x = if x <= 1 then 1 else fib (x-1) + fib (x-2);;

Factorial:

let rec fact x = if x <= 1 then 1 else x * fact (x-1);;

List Operations

Insert and Sort:

let rec insert elem = function
  | [] -> [elem]
  | h::t -> if elem < h then elem::h::t else h::insert elem t;;

let rec sort = function
  | [] -> []
  | h::t -> insert h (sort t);;

sort [4;2;3;5];;

Reverse List:

let rev list =
  let rec aux acc = function
    | [] -> 
Read More

Network Layers, Ethernet, and WAN Technologies

Application and Presentation Layer

Application and Presentation Layer: Services, files, printers, applications, databases.

Transport and Network Layer

Transport and Network Layer: Network management, routing protocols, switch.

Data Link and Physical Layers

Data Link Layer and Physical: Ethernet communications management, UTP Cat 5, SONET, physical media, hardware.

  • The first Ethernet standard was an association between Digital Equipment Corp., Intel, and Xerox (DIX).
  • Ethernet was released as an open standard.
Read More

Multiplexers, WAN, and LAN Network Technologies

Multiplexer Types

Frequency Division Multiplexer (FDM): Divides the bandwidth of a line among several channels. Each channel occupies a portion of the total frequency bandwidth.

Time Division Multiplexer (TDM): Each channel is assigned a specific time slot within the main channel. Time slots are shared equally among all channels. A disadvantage is that if a channel is unused, its time slot remains unused; other channels cannot utilize it. Padding bits are sent instead of data.

Statistical Time Division

Read More

C++ Algorithm and Data Structure Code Examples

Eratosthenes

Prime Number Checker

This code checks if a given number is prime using the Sieve of Eratosthenes algorithm.

#include #include #include using namespace std; const int Max = 1000; int main() { vector es_primer(1000001, true); es_primer[0] = es_primer[1] = false; for (int i = 2; i < Max; ++i) { if (es_primer[i]) { for (int m = 2 * i; m <= Max * Max; m += i) { es_primer[m] = false; } } } int x; while (cin >> x) { cout << x; if (es_primer[x]) cout << " es primer" <

Read More

Java Code for Queue and Stack Data Structures

Queue Class

First, we import the ArrayList class:

import java.util.ArrayList;

This is the Cola (Queue) class, which extends ArrayList:

public class Cola extends ArrayList {
    public void encolar(Object dato) {
        if (dato != null) {
            if (!esLlena())
                this.add(dato);
            else
                System.out.println("La cola esta llena.");
        } else {
            System.out.println("Introduzca un dato no nulo");
        }
    }

    public void encolar(int pos,
Read More

Pneumatic, Hydraulic, Linear, and Piezoelectric Actuators

Pneumatic Actuators

A pneumatic actuator is a mechanical device that converts compressed air pressure into mechanical motion or force. It’s commonly used in various industrial applications to control valves, dampers, gates, and other mechanical components. Pneumatic actuators are preferred for their simplicity, reliability, and suitability for high-force applications in environments where electrical actuators may not be practical or safe.

How Pneumatic Actuators Work

  1. Pneumatic Power Source: Pneumatic
Read More