小编odd*_*ddi的帖子

Haskell:用元素和列表连接一个元组列表:[[a,[b])]-> [[a,b)]

我想在一个元素和字符串元组的列表中合并,每个char与该元素一起。

例如:[[True,“ xy”),(False,“ abc”)]-> [[True,'x'),(True,'y'),(False,'a'),(False, 'b'),(False,'c')]

我确实有解决方案,但我想知道是否有更好的解决方案:

concatsplit :: [(a,[b])] -> [(a,b)]
concatsplit a = concatMap (\(x,y)-> concatsplit' (x,y)) a

concatsplit' :: (a,[b]) -> [(a,b)]
concatsplit' y = map (\x -> ((fst y),x)) (snd y)
Run Code Online (Sandbox Code Playgroud)

haskell split tuples concat list

3
推荐指数
1
解决办法
129
查看次数

Haskell:如何用文件夹减去列表中的数字?

当您使用文件夹在列表中添加数字时,它会起作用:

sumIntegers :: [Integer] -> Integer
sumIntegers xs = foldr (+) 0 xs
Run Code Online (Sandbox Code Playgroud)

但是减法的工作方式不同,因为减号minus =加号。[2,3,4,5]就像:2-3 + 4-5。

subtractNums' :: Num a => [a] -> a
subtractNums' xs = foldr (-) 0 xs

subtractNums :: Num a => [a] -> a
subtractNums []     = 0
subtractNums (x:xs) = x - subtractNums xs
Run Code Online (Sandbox Code Playgroud)

必须更改什么?先感谢您

haskell

1
推荐指数
1
解决办法
373
查看次数

标签 统计

haskell ×2

concat ×1

list ×1

split ×1

tuples ×1