将 2 个元素添加到列表的无点函数 / (:) 列表数据构造函数和 (.) 的双重应用

Ale*_*ell 0 syntax haskell functional-programming pointfree type-constructor

我正在努力正确定义该函数的无点版本,它将向列表添加 2 个元素。

\n

很容易想出一些简单的简单实现:

\n
addTwoElems :: a -> a -> [a] -> [a]\n\naddTwoElems x y xs = x : y : xs\naddTwoElems x y    = (++) [x, y]\naddTwoElems        = (.) `on` (:)  \xe2\x80\x9c point free but with additional function\n
Run Code Online (Sandbox Code Playgroud)\n

但是两个列表数据构造函数的无点组合(:)(.) 会是什么样子呢?

\n

请不仅展示正确的功能实现,还请解释如何获得正确版本的步骤和逻辑。

\n

Jos*_*ica 6

.根据评论,仅使用和的逐步推导:

addTwoElems x y xs = x : y : xs
-- rewrite the first : as a prefix function
addTwoElems x y xs = (:) x (y : xs)
-- rewrite the second : as a prefix function
addTwoElems x y xs = (:) x ((:) y xs)
-- use function composition to get xs out of the parentheses
addTwoElems x y xs = ((:) x . (:) y) xs
-- eta-reduce to get rid of xs
addTwoElems x y = (:) x . (:) y
-- rewrite the . as a prefix function
addTwoElems x y = (.) ((:) x) ((:) y)
-- use function composition to get y out of the parentheses
addTwoElems x y = ((.) ((:) x) . (:)) y
-- eta-reduce to get rid of y
addTwoElems x = (.) ((:) x) . (:)
-- rewrite the second . as an operator section, so that the part of the expression with x is last
addTwoElems x = (. (:)) ((.) ((:) x))
-- use function composition to get x out of the inner parentheses
addTwoElems x = (. (:)) (((.) . (:)) x)
-- use function composition to get x out of the outer parentheses
addTwoElems x = ((. (:)) . (.) . (:)) x
-- eta-reduce to get rid of x
addTwoElems = (. (:)) . (.) . (:)
Run Code Online (Sandbox Code Playgroud)