Music with F#: The Language and the Note

Feedbacks are more than welcome!

Looks good to me, although I’m a bit far from music :slight_smile:

Regarding the code, part below is not very effective:

let build (tonic : note) (scale : Pitch list) =
    scale 
    |> List.fold (fun notes pitch -> notes @ [ pitch (List.last notes) ]) 
           [ tonic ]

Here, you call List.last for notes every time and use operator @ for lists one of them contains only single item.

Instead, you could add a new note via operator :: and then use List.rev to return result in right order:

let build (tonic : note) (scale : Pitch list) =
    scale 
    |> List.fold (fun notes pitch -> pitch notes.Head :: notes) [ tonic ]
    |> List.rev

Btw, there is F# music library already:

Vaughan - F# library for working with music theory concepts, music notation, guitar tab notation and programmatically creating music

1 Like

Awesome, thanks for sharing.