post-cover
OCaml my Camel
First steps in FP
Written Mon Aug 14 2023
By Michael Freno
38 Hits
OCaml my Camel
First steps in FP

Preface

My programming journey is as follows:

  1. Little bit of Java in college (went for genetics- did one class with java)

  2. Started learning python in late 2021

  3. Had good understanding of python by mid 2022, started web dev - html, css, JavaScript

  4. Started with React and node in fall 2022, side focus on relearning Java

  5. Started typescript in early 2023 along with next.js

  6. Started learning rust and go and c# shortly after, not a significant focus

  7. Good understand of typescript around now :)

  8. Started focusing on rust summer 2023

  9. Now starting ocaml after some familiarity gained in rust

To give a quick rank ordering of my familiarity with each:

TS/JS > Rust > Python(although slightly rusty with it) > c# > Java > go

This is just to say that while my programming career is brief I have played around with different languages enough to know things I do and don’t like

Things I like

  • Pattern matching

  • Errors as values

  • Good build tools / ecosystems

  • Static typing

  • Inferred Types

Things I hate

  • Java

  • Paradigms as dogmas

  • Thrown exceptions

  • Dynamic typing systems

  • Inferred Type definitions

  • if err !=nil {return err}

If you have knowledge of the languages I have mentioned you can probably see the progenitors of these opinions.
Quickly, I hate the error handling of javascript with a passion, and go's while bad I don't hate, and it's kind of a meme. Dynamic typing a la python is gross and it's the main reason I don't have an interest with Elixir even though it is somewhat hot right now as far as functional programming is concerned. I hate forcing of a paradigm, everything in java/c# being a class or Haskell everything being immutable. It's bad, and leads to significant performance loss or overly convoluted code. OOP dogma leads to over abstraction thereby needless confusion and FP dogma leads to slow code.

Paradigms should be an encouraged style, but they should be bendable, OCaml allows this with mutability with ref (creates a simple record of a given type) and more complex records.

As for the good things, good pattern matching I first encountered with rust and fell in love immediately, consider this code

fn get_priority(c: char) -> Option<i32> {
    //gets an integer from a given character (value from problem prompt)
    match c {
        'a'..='z' => Some((c as i32) - ('a' as i32) + 1),
        'A'..='Z' => Some((c as i32) - ('A' as i32) + 27),
        _ => None,
    }
}

So nice. And the same logic is extremely similar in OCaml

let get_priority c =
  match c with
  | 'a'..'z' -> Char.code c - Char.code 'a' + 1
  | 'A'..'Z' -> Char.code c - Char.code 'A' + 27
  | _ -> failwith "invalid character"

So when I ended up writing this OCaml code I knew I wanted to get really really into this thing...😜

let update_top_three max_3 x =
  match max_3 with
  | [| a; b; _ |] when x > a -> [| x; a; b |]
  | [| a; b; _ |] when x <= a && x > b -> [| a; x; b |]
  | [| a; b; c |] when x <= b && x > c -> [| a; b; x |]
  | max_3 -> max_3

So that's really awesome. I'm currently running through Advent of Code (my preferred way of learning a new language) and so far, while struggling with the syntax and trying to shift off a more imperative style, I have been really enjoying the experience. Also, dune so far (the build tool for OCaml) has been surprisingly solid, and I love the name. It's cute.

Comments
No Comments Yet