As a restart, I present for your amusement a tiny expert system written in F# using lazy evaluation. The entire thing is written declaratively, with the exception of a bit of syntactic sugar in the IO (GetResponse and PrintResult). It mirrors the kind of simple expert system examples typically found in entry-level Prolog books, etc.
open System
let GetResponse q =
printf q
printf " "
let rtn = (Console.ReadKey().KeyChar='y')
printfn ""
rtn
let PrintResult s =
printfn s
true
let black = lazy (
GetResponse "Does the animal have black color?")
let fins = lazy (
GetResponse "Does the animal have fins?")
let orange = lazy (
GetResponse "Does the animal have orange color?")
let spots = lazy (
GetResponse "Does the animal have spots?")
let stripes = lazy (
GetResponse "Does the animal have stripes?")
let white = lazy (
GetResponse "Does the animal have white color?")
let blackAndOrange = lazy (
black.Value &&
orange.Value &&
PrintResult("(Asserting: black and orange.)"))
let blackAndWhite = lazy (
black.Value &&
white.Value &&
PrintResult("(Asserting: black and white.)"))
let isAFish = lazy (
fins.Value &&
PrintResult("(Asserting: is a fish.)"))
let dalmation = lazy (
spots.Value &&
blackAndWhite.Value &&
PrintResult("The animal is a dalmation."))
let leopard = lazy (
spots.Value &&
blackAndOrange.Value &&
PrintResult("The animal is a leopard."))
let tiger = lazy (
stripes.Value &&
blackAndOrange.Value &&
PrintResult("The animal is a tiger."))
let zebra = lazy (
stripes.Value &&
blackAndWhite.Value &&
not isAFish.Value &&
PrintResult("The animal is a zebra."))
let zebraFish = lazy (
stripes.Value &&
blackAndWhite.Value &&
isAFish.Value &&
PrintResult("The animal is a zebra fish."))
let animal =
dalmation.Value ||
leopard.Value ||
tiger.Value ||
zebra.Value ||
zebraFish.Value ||
PrintResult("Must be a sasquatch!")
Console.ReadLine() |> ignore
No comments:
Post a Comment