3 ms·
Typed Racket is pretty close. I imagine that with some work and Racket's macro and module system that someone could rewrite it to be more ML-like in syntax. In
by bmitc 6y ago
Typed Racket is pretty close. I imagine that with some work and Racket's macro and module system that someone could rewrite it to be more ML-like in syntax.
In the Coursera course Programming Languages, I ported some of the first assignments from SML to F# and Typed Racket, and you can see the similarities of type annotations in the very simple examples below.
SML:
(* Helper function. Takes a generic list and an integer n and returns the nth element of the list. *)
fun get_nth_element (xs : 'a list, n : int) =
if n = 1
then hd xs
else get_nth_element(tl xs, n-1)
(* Takes a list of strings and an integer n and returns the nth element of the list. *)
fun get_nth (strings : string list, n : int) = get_nth_element(strings,n)
F#:
/// Helper function. Takes a generic list and an integer n and returns the nth element of the list.
let rec getNthElement (xs : 'a list, n : int) =
if n = 1
then List.head xs
else getNthElement(List.tail xs, n-1)
/// Takes a list of strings and an integer n and returns the nth element of the list.
let getNth (strings : string list, n : int) = getNthElement(strings,n)
Racket:
#lang typed/racket
;; Helper function. Takes a generic list and an integer n and returns the nth element of the list.
(: get-nth-element (All (T) (Listof T) Integer -> T))
(define (get-nth-element xs n)
(if (equal? n 1)
(first xs)
(get-nth-element (rest xs) (- n 1))))
;; Takes a list of strings and an integer n and returns the nth element of the list.
(: get-nth ((Listof String) Integer -> String))
(define (get-nth strings n) (get-nth-element strings n))