Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created April 16, 2009 04:34
Show Gist options
  • Save mattpodwysocki/96219 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/96219 to your computer and use it in GitHub Desktop.
#light
[<AutoOpen>]
module Operators =
// Untuple forward operator
let (||>) (x,y) f = f x y
module Map =
let union m1 m2 =
(m1, m2) ||> Map.fold_right Map.add
let intersect m1 m2 =
(m2, Map.empty)
||> Map.fold_right (fun k _ acc ->
match Map.tryfind k m1 with
| None -> acc
| Some x -> Map.add k x acc)
// Returns a distance-based similarity score for p1 and p2
let sim_distance (prefs:Map<string, Map<string, float>>) p1 p2 =
// Get the list of shared items
let si = Map.intersect prefs.[p1] prefs.[p2] |> Seq.map (fun kvp -> kvp.Key)
// No ratings in common, return 0
match Seq.length si with
| 0 -> 0.
| _ ->
// Add up the squares of all the differences
let sum_of_squares =
seq {for item in si -> pown (prefs.[p1].[item] - prefs.[p2].[item]) 2}
|> Seq.sum
1. / (1. + (sqrt sum_of_squares))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment