Advertisement
Guest User

Untitled

a guest
Oct 12th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.04 KB | None | 0 0
  1. type Client =
  2.     { Name : string;
  3.       Income : int;
  4.       YearsInJob : int;
  5.       UsesCreditCard : bool;
  6.       CriminalRecord : bool
  7.     }
  8.  
  9. let john = {Name = "John Doe"; Income = 40000; YearsInJob = 1; UsesCreditCard = true; CriminalRecord = false}
  10.  
  11. let creditTests =
  12.     [(fun cl -> cl.CriminalRecord = true);
  13.      (fun cl -> cl.Income < 30000);
  14.      (fun cl -> cl.UsesCreditCard = false);
  15.      (fun cl -> cl.YearsInJob < 2)]
  16.  
  17. let testClient client =
  18.     let issues = creditTests |> List.filter (fun f -> f (client))
  19.     let suitable = issues.Length <= 1
  20.     printf "Client: %s\n Offer a loan: %s (issues = %d)" client.Name (if suitable then "YES" else "NO") issues.Length
  21.  
  22. testClient john
  23.  
  24. type QueryInfo =
  25.     { Title : string
  26.       Check : Client -> bool
  27.       Positive : Decision
  28.       Negative : Decision
  29.     }
  30. and Decision =
  31.     | Result of string
  32.     | Query of QueryInfo
  33.  
  34. let rec tree =
  35.     Query ( {Title = "More than 40k"
  36.              Check = (fun cl -> cl.Income > 40000)
  37.              Positive = moreThan40; Negative = lessThan40})
  38. and moreThan40 =
  39.     Query({Title = "Has criminal record"
  40.            Check = (fun cl -> cl.CriminalRecord)
  41.            Positive = Result("NO"); Negative = Result("YES")})
  42. and lessThan40 =
  43.     Query({Title = "Year in job"
  44.            Check = (fun cl -> cl.YearsInJob > 1)
  45.            Positive = Result("YES"); Negative = usesCredit})
  46. and usesCredit =
  47.     Query({Title = "Uses credit card"
  48.            Check = (fun cl -> cl.UsesCreditCard)
  49.            Positive = Result("YES"); Negative = Result("NO")})    
  50.  
  51. let rec testClientTree tree client =
  52.     match tree with
  53.     | Result(message) ->
  54.         printfn " OFFER A LOAN: %s" message
  55.     | Query(qinfo) ->
  56.         let result, case =
  57.             if (qinfo.Check(client)) then "yes", qinfo.Positive
  58.             else "no", qinfo.Negative
  59.         printfn " - %s? %s" qinfo.Title result
  60.         testClientTree case client
  61.  
  62. testClientTree tree john
  63.  
  64. let listOfClients = [john]
  65.  
  66. listOfClients |> List.map (testClientTree tree)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement