Skip to content

Instantly share code, notes, and snippets.

@gabyfle
Created March 5, 2021 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabyfle/d7d727bb7eeb7bdb13a13b9e7bb1cea9 to your computer and use it in GitHub Desktop.
Save gabyfle/d7d727bb7eeb7bdb13a13b9e7bb1cea9 to your computer and use it in GitHub Desktop.
TD2
open Printf
let rec ackermann n p =
if n = 0 then p + 1
else if p = 0 then ackermann (n - 1) 1
else ackermann (n - 1) (ackermann n (p - 1))
(* Returns the list's head *)
let head = function
| [] -> failwith "Incorrect list, sorry man"
| h :: _ -> h
(* Returns the list's tail *)
let tail = function
| [] -> failwith "Incorrect list, sorry man"
| _ :: t -> t
(* Returns the list's length *)
let rec length = function
| [] -> 0
| h :: t -> 1 + (length t)
(* Returns the list's maximum element *)
let rec max = function
| [] -> failwith "Empty list"
| [x] -> x
| h :: t ->
let maximum = max t in
if h > maximum then h else maximum
(* Returns the n-th element of the list *)
let rec element l n = match l, n with
| [], _ -> failwith "Invalid list"
| h :: t, 1 -> h
| _ :: t, n -> element t (n - 1)
(* Reverse list elements *)
let reversal a =
let rec reverse l m = match l with
| [] -> m
| h :: t -> reverse t (h :: m)
in
reverse a []
(* Concatenate two lists *)
let concat a b =
let reversed = reversal a in
let rec concat_aux l m = match l with
| [] -> m
| h :: t -> concat_aux t (h :: m)
in
concat_aux reversed b
(* let say that powers appears only one time each *)
let rec degree n = function
| [] -> n
| h :: t -> match h with
| (0., _) -> degree n t
| (_, b) when b > n -> degree b t
| (_, _) -> degree n t
(* we admit that powers appear in ascending order one time each
let get_first (a, _) = a
let rec coefficient p deg = match p, deg with
| [], _ -> failwith "issue with list length"
| h :: t, 0 -> get_first h
| h :: t, n -> coefficient t (n - 1)
*)
let coefficient p deg =
let rec element p deg = match p, deg with
| [], _ -> failwith "invalid list"
| h :: t, 0 -> h
| h :: t, n -> element t (n - 1)
in
match (element p deg) with (a, _) -> a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment