haskell - 需要一个函数来验证密码是否强
问题描述
我需要创建一个函数来验证密码是否强大。如果有超过 8 个字符,至少 1 个大写字母、1 个小写字母和 1 个数字。但是我的功能不起作用。有什么问题?
forte :: String -> Bool
forte s = if (length n >= 8) && (isLower s /= 0) && (isUpper s /= 0) && (isDigit s /= 0)
then True
else False
解决方案
欢迎来到stackoverflow。将来,当您说“不起作用”时,请具体说明。复制和粘贴错误消息是一个很好的开始。
对于您的情况,有一些问题。
- 没有变量
n
。我想你的意思s
是在length s
. - 该
isLower
函数对字符而不是字符串(字符列表)进行操作。您应该检查过滤列表的长度 (length (filter isLower s) /= 0
)。isUpper
和 也是如此isDigit
。 - 顺便说一句,该
if
声明是完全不需要的。任何时候你写if expr then True else False
它都和只是写一样expr
。
应用这些建议,我们有:
forte s = (length s >= 8) && (length (filter isLower s) /= 0) && (length (filter isUpper s) /= 0) && (length (filter isDigit s) /= 0)
或使用辅助功能:
forte s = (length s >= 8) && (cnt isLower /= 0) &&
(cnt isUpper /= 0) && (cnt isDigit /= 0)
where cnt p = length (filter p s)
但我可能会把它写成(输入但未测试):
forte s = and [ length s >= 8
, cnt isLower /= 0
, cnt isUpper /= 0
, cnt isDigit /= 0]
where cnt p = length (filter p s)
编辑:啊,我不知道为什么我没有考虑任何而不是计算元素。感谢@lorenzo
forte s = and [ length s >= 8
, any isLower s
, any isUpper s
, any isDigit s]
或者使用 all 和 any,尽管这需要您了解$
函数组合 ( .
) 的部分应用:
forte s = all ($ s) [ (>= 8) . length
, any isLower
, any isUpper
, any isDigit ]
推荐阅读
- eigen - 编译 Eigen 以在 QNX 6 上运行
- httprequest - 尝试通过使用 VB 脚本发送 json 数据来发出 http post 请求并期待响应
- r - 如何使 geom_line() 在时间序列图中更平滑
- css - css - 卡片块,如何定位和调整图像大小
- android - 在颤动中重复相邻的图像
- python-3.x - PyQt5 - QTableWidget 水平滚动条
- python-3.x - 如何使用 shutil.rmtree.avoids_symlink_attacks?
- go - gl.DrawElements 似乎使用了不正确的索引
- java - 尝试启动真正的安卓手机的安卓应用程序时出现“未知的服务器端错误”
- python-3.x - 寻找更有效的算法