将两个有序的数字列表合并为一个递归

时间:2018-02-19 21:20:29

标签: list haskell recursion merge pattern-matching

我是Haskell的新手,递归 例如输入[1,3,4,5] [6,9,8,10] 输出= [1,3,4,5,6,8,9,10] 我以前的尝试是

>let addlist [] [] = []

>let addlist (a:as) (b:bs) = (a+b) : addlist as bs
>let x = [1..5]
>let y = [6..10]
>addlist x y

但是这会在列表中添加数字而不是合并它们。

1 个答案:

答案 0 :(得分:2)

怎么样

N(0,2)

相当于

let { addlist (a:as) (b:bs) = if  a<= b then  a : addlist as (b:bs) else  b : addlist (a:as) bs; 
addlist [] bs = bs; 
addlist as [] = as }