如果有帮助,这里是显式递归:
type No = String
data Arco = Arco { de :: No
, para :: No
, custo :: Float
} deriving (Show, Ord, Eq)
my_func xs = helper xs []
where helper [] acc = reverse acc
helper (x:xs) acc = let key = (de x, para x)
val = custo x
in helper xs ( (key, val):acc )
ghci>:l 1.hs
ghci>let a = Arco "hello" "world" 3.0
ghci>let b = Arco "goodbye" "mars" 10.0
ghci>let list = my_func [a, b]
ghci>list
[(("hello","world"),3.0),(("goodbye","mars"),10.0)]
ghci>import qualified Data.Map as M
ghci>let map = M.fromList list
ghci>map
fromList [(("goodbye","mars"),10.0),(("hello","world"),3.0)]
ghci>M.lookup ("hello", "world") map
Just 3.0