Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String '有什么用$';Haskell中的登录字符串操作?_String_Haskell - Fatal编程技术网

String '有什么用$';Haskell中的登录字符串操作?

String '有什么用$';Haskell中的登录字符串操作?,string,haskell,String,Haskell,此代码根据要求中的约束检查给定密码是否有效 import Data.Char strong :: String -> Bool strong password = all ($ password) requirements where requirements = [minLength 15, any isUpper, any isLower, any isDigit] minLength n str = n <= length str 导入数据.Cha

此代码根据要求中的约束检查给定密码是否有效

import Data.Char

strong :: String -> Bool
strong password = all ($ password) requirements
  where requirements = [minLength 15, any isUpper, any isLower, any isDigit] 
        minLength n str = n <= length str  
导入数据.Char
strong::String->Bool
强密码=所有($password)要求
其中requirements=[minLength 15,any isUpper,any isLower,any isDigit]
最小长度n str=n
根据
all
的定义,上述内容等同于

($ password) (minLength 15) &&
($ password) (any isUpper) &&
($ password) (any isLower) &&
($ password) (any isDigit)
现在,在Haskell中,一个所谓的节
(+-*/x)
代表
(\y->y+-*/x)
,无论操作符是什么
+-*/
(在您的例子中是
$
)。因此,我们获得

(minLength 15 $ password) &&
(any isUpper $ password) &&
(any isLower $ password) &&
(any isDigit $ password)
最后,操作符
$
的定义如下:
f$x=f x
,即它是函数应用操作符。我们将上述代码进一步简化为:

minLength 15 password &&
any isUpper password &&
any isLower password &&
any isDigit password
$
表示“功能应用”

$
可用于为函数提供参数。在这种情况下,您可以使用它将“密码”应用于“要求”所具有的每个功能,并具有以下类型:

-- because $ is infix, left and right param are the 1st and 2nd respectively
-- partial apply with the right param will still need the left one (a->b)
:t ( $ "a")    
( $ "a") :: ([Char] -> b) -> b --where "b" is a bool in this case
看起来很熟悉吗

:t (\f -> f "a")
(\f -> f "a") :: ([Char] -> b) -> b
$password
(\f->f password)
具有相同的类型和行为:获取一个函数并将其应用于
a

当您必须在列表上应用一个函数时,您只需将该函数单独放置即可。 但在这种情况下,您必须将列表中的函数应用于“密码”。如果省略
$
它将尝试执行“password(any isLower)”,例如,这没有任何意义


但是您可以使用类似于
all(\f->f password)
的lambda将每个函数应用于
password
,或者您可以使用
$

如果我告诉您
$password
(\f->f password)
相同,这对您有帮助吗?也许你可以从那里找到答案。@JoachimBreitner这是一条很酷的捷径!谢谢知道了。很好的解释。@bunny9131你能接受我的回答吗?还是另一个?
-- because $ is infix, left and right param are the 1st and 2nd respectively
-- partial apply with the right param will still need the left one (a->b)
:t ( $ "a")    
( $ "a") :: ([Char] -> b) -> b --where "b" is a bool in this case
:t (\f -> f "a")
(\f -> f "a") :: ([Char] -> b) -> b