递归合并F#中的两个列表 [英] Merge Two Lists in F# Recursively

查看:88
本文介绍了递归合并F#中的两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望编写一个递归函数以合并到F#中的整数列表中

I am looking to write a recursive function to merge to integer lists in F#

我从此开始,但不确定下一步该怎么做.

I started with this, but not sure what to do next.

let rec merge xs ys =
    match xs with
    | [] -> ys
    | 

let li = [1;3;5;7;]
let ll = [2;4;5;8;]

推荐答案

正如我在评论中所说,如果同时在xs和ys上进行模式匹配,那可能是最简单的:

As I said in my comment, it's probably easiest if you pattern match on xs and ys simultaneously:

let rec merge xs ys = 
  match xs,ys with
  | [],l | l,[] -> l
  | x::xs', y::ys' -> 
     if x < y then x :: (merge xs' ys) //'
     else y :: (merge xs ys')          //'

这篇关于递归合并F#中的两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆