Advertisement
ptrelford

Harry likes Sally

Feb 21st, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.02 KB | None | 0 0
  1. type Entity = Entity of string
  2. type Relationship = Relationship of string
  3.  
  4. let entity name = Entity(name)
  5. let relationship name = Relationship(name)
  6. let __ = entity("__")
  7.  
  8. let facts = ref []
  9.  
  10. let fact (rel:Relationship) (xs:Entity list) =
  11.     facts := (rel,xs) :: !facts
  12.  
  13. let question (Relationship(rel)) (clause:Entity list) =
  14.     let find clause clause' =
  15.        let xs = List.zip clause clause'
  16.         if xs |> List.forall (fun (a,b) -> a = __ || a = b)
  17.         then xs |> List.tryPick (fun (a,b) -> if a = __ then Some b else None)
  18.         else None
  19.     [for (Relationship(rel'),clause') in !facts do
  20.         if rel = rel' then
  21.            match find clause clause' with
  22.             | Some(answer) -> yield answer
  23.             | None -> ()        
  24.     ]
  25.  
  26. let sally = entity "sally"
  27. let harry = entity "harry"
  28. let likes = relationship "likes"
  29.  
  30. fact likes [harry;sally]
  31.  
  32. question likes [harry;__]
  33. // val it : Entity list = [Entity "sally"]
  34. question likes [__;sally]
  35. // val it : Entity list = [Entity "harry"]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement