F# Creating function for comparing two lists containing lists -
i'm trying create program allows user check if person interested in going arrangement. have types
type interestlist = { interest : string; } type description = { name : string; phone : int birthdate : int interests : list<interestlist> } type register = { regid : list<description> } type arrangement = { year : int arrinterests : list<interestlist> }
if use register containing people i'd check
let reg = [ ("steven", 11111111, 1991, (["soccer", "flowers", "jazz"])) ("carl", 22222222, 1842, (["animals", "shopping", "soccer"])) ("karen", 33333333, 2005, (["animals", "volleyball", "jazz"])) ];;
and have 2 arrangements
let p1 = [ (1982, (["soccer", "jazz"])) ];; let p2 = [ (1998, (["soccer"])) (1998, (["jazz"])) ];;
a person interested in going arrangement if age greater age restraint on arrangement (so if person born in 1982 , arrangement has 1987, criteria fulfilled) , if interests of arrangement matches or of interests of person. i'd create function extractinterested taking register , arrangement arguments. anyone?
edit:
i've tried along lines of
let extractinterested f (person : register list, arr : arrangement list) = if birthyear > year (person |> list.map (fun x -> (x, list.tryfind (f x) arr)) |> list.iter (function (x, none) -> printfn "%a not interested in going arrangement" x | (x, y) -> printfn "%a interested in going arrangement" x) )
this should extent compare 2 (the register of people , arrangement),
if birthyear > year
doesn't seem work.
this getting confused comments because question isn't specific. i'll try split more sharply defined bits.
step 1: suitable data structure given problem?
assuming interest merely string, need types person, register, , arrangement. it'll similar types in question, aren't clear me, let's modify them bit:
type person = { name : string phonenumber : int birthyear : int interests : set<string> } type arrangement = { year : int coveredinterests : set<string> } type register = { people : person list }
i use sets aggregating interests, since disallows duplicates , simplifies identification of common interests via set intersection.
step 2: should algorithm match people arrangements like?
the main functionality here determine whether person interested or not, function isinterested
main bit. extractinterested
function filter makes use of isinterested
.
let isinterested arrangement person = let hascommoninterest = set.intersect person.interests arrangement.coveredinterests |> set.isempty |> not hascommoninterest && person.birthyear < arrangement.year let extractinterested register arrangement = list.filter (isinterested arrangement) register.people
note use of partial application: (isinterested arrangement)
function answers whether specific person interested in specific arrangement.
step 3: how instances created , used?
this f# record , list syntax.
let steven = { name = "steven"; phonenumber = 11111111; birthyear = 1991 interests = set ["soccer"; "flowers"; "jazz"] } let carl = { name = "carl"; phonenumber = 22222222; birthyear = 1842 interests = set ["animals"; "shopping"; "soccer"] } let karen = { name = "karen"; phonenumber = 33333333; birthyear = 2005 interests = set ["animals"; "volleyball"; "jazz"] } let mainregister = { people = [steven; carl; karen] } let arrangement1 = { year = 1982; coveredinterests = set ["soccer"; "jazz"] }
using via extractinterested mainregister arrangement1
return carl's instance, has common interest of soccer , person entry birth year before 1982.
a hint getting useful stackoverflow answers
the broader question is, harder answer, it's useful split problems small, specific questions. if keep abstract , small, you'll find answered somewhere, , remaining cases, people more give detailed advice problematic part.
solving problem might begin questions "what data structure should use set of interests?" or "how create instances of record type created?". i'd recommend first determine , answer such questions using documentation or search, , if hit roadblock, ask specific question obstacle you're struggling with.
Comments
Post a Comment