Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Client =
- { Name : string;
- Income : int;
- YearsInJob : int;
- UsesCreditCard : bool;
- CriminalRecord : bool
- }
- let john = {Name = "John Doe"; Income = 40000; YearsInJob = 1; UsesCreditCard = true; CriminalRecord = false}
- let creditTests =
- [(fun cl -> cl.CriminalRecord = true);
- (fun cl -> cl.Income < 30000);
- (fun cl -> cl.UsesCreditCard = false);
- (fun cl -> cl.YearsInJob < 2)]
- let testClient client =
- let issues = creditTests |> List.filter (fun f -> f (client))
- let suitable = issues.Length <= 1
- printf "Client: %s\n Offer a loan: %s (issues = %d)" client.Name (if suitable then "YES" else "NO") issues.Length
- testClient john
- type QueryInfo =
- { Title : string
- Check : Client -> bool
- Positive : Decision
- Negative : Decision
- }
- and Decision =
- | Result of string
- | Query of QueryInfo
- let rec tree =
- Query ( {Title = "More than 40k"
- Check = (fun cl -> cl.Income > 40000)
- Positive = moreThan40; Negative = lessThan40})
- and moreThan40 =
- Query({Title = "Has criminal record"
- Check = (fun cl -> cl.CriminalRecord)
- Positive = Result("NO"); Negative = Result("YES")})
- and lessThan40 =
- Query({Title = "Year in job"
- Check = (fun cl -> cl.YearsInJob > 1)
- Positive = Result("YES"); Negative = usesCredit})
- and usesCredit =
- Query({Title = "Uses credit card"
- Check = (fun cl -> cl.UsesCreditCard)
- Positive = Result("YES"); Negative = Result("NO")})
- let rec testClientTree tree client =
- match tree with
- | Result(message) ->
- printfn " OFFER A LOAN: %s" message
- | Query(qinfo) ->
- let result, case =
- if (qinfo.Check(client)) then "yes", qinfo.Positive
- else "no", qinfo.Negative
- printfn " - %s? %s" qinfo.Title result
- testClientTree case client
- testClientTree tree john
- let listOfClients = [john]
- listOfClients |> List.map (testClientTree tree)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement