DEV Community

loading...

Discussion on: Daily Challenge #48 - Facebook Likes

Collapse
citizen428 profile image
Michael Kohl • Edited

F#:

let likes names =
    match names with
    | [] -> "no one likes this"
    | [ a ] -> sprintf "%s likes this" a
    | [ a; b ] -> sprintf "%s and %s like this" a b
    | [ a; b; c ] -> sprintf "%s, %s and %s like this" a b c
    | a :: b :: rest -> sprintf "%s, %s and %d others like this" a b rest.Length

Alternatively a point-free version without explicit argument, though it's considered bad style for exported functions:

let likes =
    function
    | [] -> "no one likes this"
    | [ a ] -> sprintf "%s likes this" a
    | [ a; b ] -> sprintf "%s and %s like this" a b
    | [ a; b; c ] -> sprintf "%s, %s and %s like this" a b c
    | a :: b :: rest -> sprintf "%s, %s and %d others like this" a b rest.Length