Haskell:通过“简化函数”;“有原则的转变”;

Haskell:通过“简化函数”;“有原则的转变”;,haskell,transformation,Haskell,Transformation,在过去的一两周里,在我的计算机科学课上,有人问我们如何通过“原则转换”来简化和缩短函数。我们一直没有收到关于这些作业的反馈,所以我不知道我是否做对了 以下是我的最新练习,以及我对解决方案的尝试: Show by a series of principled transformations that we can define: char :: Char -> Parser Char char c = satisfy (c==) as char :: Char -&g

在过去的一两周里,在我的计算机科学课上,有人问我们如何通过“原则转换”来简化和缩短函数。我们一直没有收到关于这些作业的反馈,所以我不知道我是否做对了

以下是我的最新练习,以及我对解决方案的尝试:

Show by a series of principled transformations that we can define:
    char :: Char -> Parser Char
    char c = satisfy (c==)
as
    char :: Char -> Parser Char
    char = satisfy . (==)
我的尝试:

char c = satisfy (c==)
=> char c = satisfy . c==
=> char c = satisfy . flip ==c
=> char = satisfy . flip ==
=> char = satisfy . (==)

我能得到一些反馈吗?为赋值提供的代码不完整,因此我无法编译它并测试每个转换是否有效。我试着自己编写一组类似的函数来测试转换,但不幸的是,我对Haskell非常不熟悉,所以我也无法理解这一点。

这里有一些错误,首先我要写下相关的类型

char :: Char -> Parser Char
satisfy :: (Char -> Bool) -> Parser Char
(==) :: Char -> Char -> Char
我特意限制了一些签名,使之更令人愉快

char c = satisfy (c==)
char c = satisfy . c== -- This is a parse error, not sure what you meant
char c = satisfy . flip ==c -- Also wrong, flip swaps arguments, 
                            -- but this function has only one argument
char = satisfy . flip == -- Eta conversion is right, this code is
                         -- still a parse error - you should check code with ghci or winhugs
我的方法是

char c = satisfy   (c==)
char c = satisfy $ (\c -> \d -> c == d) c -- Explicitly undo sections
char c = satisfy . (\c d -> c == d) $ c -- f(g x) === (f . g) x by the
                                        -- definition of composition
char c = (satisfy . (==)) c
char = satisfy . (==)

以下是一个逐步的方法:

char c = satisfy (c==)
char c = satisfy (\x -> c == x)   -- Sections [1]
char c = satisfy (\x -> (==) c x) -- Prefix form
char c = satisfy ((==) c)         -- Eta reduction [2]
char c = (.) satisfy (==) c       -- Composition: `(.) f g x = f (g x)`
char c = (satisfy . (==)) c       -- Infix form
char   = satisfy . (==)           -- Eta reduction
我甚至可能放弃显式地扩展这个部分,而只是从
(c==)
转到
((==)c)

1:
2: