我正在尝试将以下 Haskell 代码转换为 Javascript:
data These a b = This a | That b | These a b
class Align f where
align :: (These a b -> c) -> f a -> f b -> f c
instance Align [] where
align f [] [] = []
align f (x:xs) [] = f (This x) : align f xs []
align f [] (y:ys) = f (That y) : align f [] ys
align f (x:xs) (y:ys) = f (These x y) : align f xs ys
liftAlign2 f a b = align t
where t (This l) = f l b
t (That r) = f a r
t (These l r) = f l r
zipPad a b = liftAlign2 (,) a b
我在翻译这个liftAlign2函数时遇到了麻烦。它需要一个元组构造函数和两个默认值,但我不知道从何t而来。这是我到目前为止的代码:
const union = type => (tag, o) =>
(o.type = type.name || type, o.tag = tag.name || tag, o);
const These_ = union("These");
const This = _this => These_(This, {this: _this});
const That = that => These_(That, {that});
const These = _this => that => These_(These, {this: _this, that});
const Pair = x => y => [x, y];
const align = f => ([x, ...xs]) => ([y, ...ys]) =>
x === undefined && y === undefined ? []
: y === undefined ? [f(This(x)), ...align(f) (xs) ([])]
: x === undefined ? [f(That(y)), ...align(f) ([]) (ys)]
: [f(These(x) (y)), ...align(f) (xs) (ys)];
const liftAlign2 = f => x => y => ?
const zipPad = x => y =>
liftAlign2(Pair) (x) (y);
const zipPad ("") (0) (["foo", "bar"]) ([2, 4, 6]); // [["foo", 2], ["bar", 4], ["", 6]]
我知道 JS 代码效率很低,因为它不处理函数List类型,而是处理数组。效率对于这种翻译并不重要。
慕雪6442864
慕丝7291255
相关分类