Haskell编写你的($)函数的版本 [英] Haskell write your version of a ($) function
问题描述
($)
函数吗? 这是我的尝试,不起作用。
f函数(x:xs)= f(x:xs)
首先,您为什么认为您需要:
或 foldr
在这里?这些对列表是排他性的。 $
与列表无关。
f function(x)= f x)
更喜欢它,但是您似乎对 f
作为参数,反之亦然。事实上, f函数(x)= f(x)
表示:
f = \ function x - > f x
即您将 f
定义为一个函数,该函数接受一个名为的函数
(这是从未实际使用的)和另一个参数 x
然后它将你正在定义的函数应用到这里......这种递归结合在Haskell中实际上是可能的(有时非常有用),但在这种情况下,它没有任何意义。
实际上你想写的内容要简单得多:
f $ x = fx
请注意<$ c因为它由非字母符号组成,因此是一个 infix ,因此分析方式不同:
<$> pre> ($)= \ fx - > fx
也就是说, $
和一个参数并将该函数应用于参数;就是这样。
你也可以通过使用反引号将字母名称分解为中缀来实现。
f`function` x = fx
...或者简单地
pre $ code>函数fx = fx
Could someone help me with writing my own version of the ($)
function?
This is my attempt which does not work.
f function (x:xs) = f (x:xs)
First, why do you think you need :
or foldr
here? These are exclusive to lists. $
has nothing to do with lists.
f function (x) = f (x)
is more like it, however it seems you're confused about whether you defining function
with f
as an argument, or vice versa. In fact, f function (x) = f (x)
means this:
f = \function x -> f x
i.e. you're defining f
as a function which takes an argument called function
(that is never actually used) and another argument x
to which it then applies the very function you're defining right here... this kind of recursive knot-tying is actually possible in Haskell (and sometimes quite useful), but in this case it doesn't make any sense.
What you actually want to write is much simpler:
f $ x = f x
note that $
, because it consists of a non-letter symbol, is an infix and therefore parsed differently:
($) = \f x -> f x
Which means, $
takes a function and an argument and applies the function to the argument; that's it.
You could also achieve this with a name with letters by using backticks to have it parsed as an infix:
f `function` x = f x
...or simply
function f x = f x
这篇关于Haskell编写你的($)函数的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!