F#:声明函数并在列表中查找元素
F#: Declare function and find element in list
我是 F# 的新手,遇到问题需要一些帮助。我在查找列表中的元素时遇到问题:
"Declare a function findRoute: Lid*LuggageCatalogue -> Route, that finds the route for a given luggage identification in a luggage catalogue"
我列出了不同的类型并给出了类型元素。
type Lid = string
type Flight = string
type Airport = string
type Route = (Flight*Airport) list
let route = [("DL 189","ATL"); ("DL 124","BRU"); ("SN 733","CPH"); ("SK 208","ATL"); ("DL 124","BRU"); ("SK 122","JFK")]
type LuggageCatalogue = (Lid*Route) list
let lc = [("DL 016-914", [("DL 189","ATL"); ("DL 124","BRU"); ("SN 733","CPH")]);
("SK 222-142", [("SK 208","ATL"); ("DL 124","BRU"); ("SK 122","JFK")])]
let findRoute fr = function
match fr with
| [] -> printf "No match"
|
我卡住的地方是我找不到使用该函数在 LuggageCatalogue
列表
中查找元素的正确方法
也许您可以先过滤行李目录以更好地了解发生了什么,例如
let filterlist lid = List.filter (fun (luggageid, _) -> luggageid = lid)
然后在目录中查找特定项目就像
filterlist "SK 222-142" lc
或者更好的是,尝试自己创建函数的第一部分。尽管这只是第一步,也许可以尝试想象一下行李 id 的递归搜索功能的外观,
let findfirst lid =
let rec checktail lctail =
match lctail with
| [] -> invalidArg "lid" "luggageid not present in catalogue"
| (id, r)::tail -> if id = lid then r else checktail tail
checktail lc
我是 F# 的新手,遇到问题需要一些帮助。我在查找列表中的元素时遇到问题: "Declare a function findRoute: Lid*LuggageCatalogue -> Route, that finds the route for a given luggage identification in a luggage catalogue"
我列出了不同的类型并给出了类型元素。
type Lid = string
type Flight = string
type Airport = string
type Route = (Flight*Airport) list
let route = [("DL 189","ATL"); ("DL 124","BRU"); ("SN 733","CPH"); ("SK 208","ATL"); ("DL 124","BRU"); ("SK 122","JFK")]
type LuggageCatalogue = (Lid*Route) list
let lc = [("DL 016-914", [("DL 189","ATL"); ("DL 124","BRU"); ("SN 733","CPH")]);
("SK 222-142", [("SK 208","ATL"); ("DL 124","BRU"); ("SK 122","JFK")])]
let findRoute fr = function
match fr with
| [] -> printf "No match"
|
我卡住的地方是我找不到使用该函数在 LuggageCatalogue
列表
也许您可以先过滤行李目录以更好地了解发生了什么,例如
let filterlist lid = List.filter (fun (luggageid, _) -> luggageid = lid)
然后在目录中查找特定项目就像
filterlist "SK 222-142" lc
或者更好的是,尝试自己创建函数的第一部分。尽管这只是第一步,也许可以尝试想象一下行李 id 的递归搜索功能的外观,
let findfirst lid =
let rec checktail lctail =
match lctail with
| [] -> invalidArg "lid" "luggageid not present in catalogue"
| (id, r)::tail -> if id = lid then r else checktail tail
checktail lc