Haskell-使用不同函数迭代元组

Haskell-使用不同函数迭代元组,haskell,Haskell,我一直在尝试迭代cand数据,以便应用函数“pt_string” 我的想法是对tuple的每个元素以不同的方式调用函数“pt_string” 例如: pt_字符串点(第一个) 地图点字符串[点] pt_字符串点(秒) 显示“Tmp” 到目前为止,我得到: pt_string :: pt -> String pt_string pt = "(" ++ show (fst pt) ++ "," ++ show (snd pt) ++ ")\n" 这很好用。但是如何按照上述顺序创建cand\

我一直在尝试迭代cand数据,以便应用函数“pt_string”

我的想法是对tuple的每个元素以不同的方式调用函数“pt_string”

例如:

  • pt_字符串
    (第一个)
  • 地图点字符串
    [点]
  • pt_字符串
    (秒)
  • 显示“Tmp”
  • 到目前为止,我得到:

    pt_string :: pt -> String
    pt_string pt =  "(" ++ show (fst pt) ++ "," ++ show (snd pt) ++ ")\n"
    
    这很好用。但是如何按照上述顺序创建cand\u to\u string::cand->string

    谢谢

    假设

    type Candidate = (Point, Point, [Point], Float)
    
    你可以用

    candidate_to_string :: Candidate -> String
    candidate_to_string (p1, p2, ps, f) =
       "(" ++
       point_to_string p1 ++ ", " ++
       point_to_string p2 ++ ", " ++
       points_to_string ps ++ ", " ++
       show f ++
       ")"
    
    这取决于

    points_to_string :: [Point] -> String
    points_to_string ps = "[" ++ intercalate ", " (map point_to_string ps) ++ "]"
    
    利用
    Data.List.interlate
    在点之间添加逗号

    还请注意,如果您只是想要标准列表/元组打印格式,可以直接使用

    candidate_to_string :: Candidate -> String
    candidate_to_string = show
    
    candidate_to_string :: Candidate -> String
    candidate_to_string = show