非穷举模式匹配只是因为我遗漏了`否则=`?(Non-exhaustive pattern matches only because I left off `otherwise =`? [duplicate])
这个问题在这里已经有了答案:
- GHC为什么抱怨非穷举模式? 3个答案
我在Haskell中编写了一个简单的程序,它可以在Rust编程语言手册中描述玩猜谜游戏 :
以下是它的工作原理:程序将生成一个1到100之间的随机整数。然后它会提示玩家输入猜测。 输入猜测后,它会指示猜测是否过低或过高。 如果猜测是正确的,游戏将打印祝贺并退出。
这是我写的:
import Control.Monad (when) import System.Random (randomRIO) -- | Check if the guess is correct, otherwise provide a hint respond :: Int -> Int -> String respond correct guess | guess > correct = "Smaller than " ++ show guess | guess < correct = "Larger than " ++ show guess | guess == correct = "Correct! " ++ show correct -- | Main game loop; prompt for input and repeat until guessed correctly play :: Int -> IO () play x = do putStr "Guess: " guess <- read <$> getLine putStrLn $ respond x guess when (guess /= x) $ play x -- | Start the game with a random number between 1 and 100 main :: IO () main = play =<< randomRIO (1, 100)
代码有效,但是GHC给了我一个警告:
"Pattern match(es) are non exhaustive. In an equation for 'respond': Patterns not matched: _ _"
我将这两个下划线表示我作为
respond
函数参数的两个Ints
。 我不明白的是我没有涉及的情况。 这些不是Maybe Int
或任何特殊的 - 函数需要两个有效的Ints
,所以我只需要处理整数 - 我不认为有任何数字不能被视为大于,小于或等于另一个?这只是GHC,假设我没有覆盖所有的情况,因为我没有添加最终的
otherwise =
后卫? 尽管它在逻辑上涵盖了所有情况。
另外,如果你有关于如何编写更多地道Haskell的技巧,我会很感激他们。 我仍然在学习基础知识。
This question already has an answer here:
I wrote a simple program in Haskell that plays the guessing game described in The Rust Programming Language book:
Here’s how it works: the program will generate a random integer between 1 and 100. It will then prompt the player to enter a guess. After entering a guess, it will indicate whether the guess is too low or too high. If the guess is correct, the game will print congratulations and exit.
Here is what I wrote:
import Control.Monad (when) import System.Random (randomRIO) -- | Check if the guess is correct, otherwise provide a hint respond :: Int -> Int -> String respond correct guess | guess > correct = "Smaller than " ++ show guess | guess < correct = "Larger than " ++ show guess | guess == correct = "Correct! " ++ show correct -- | Main game loop; prompt for input and repeat until guessed correctly play :: Int -> IO () play x = do putStr "Guess: " guess <- read <$> getLine putStrLn $ respond x guess when (guess /= x) $ play x -- | Start the game with a random number between 1 and 100 main :: IO () main = play =<< randomRIO (1, 100)
The code works, but GHC gives me a warning that
"Pattern match(es) are non exhaustive. In an equation for 'respond': Patterns not matched: _ _"
I take those two underscores to represent the two
Ints
I have as arguments to therespond
function. What I don't understand is which case I haven't covered. Those aren'tMaybe Int
s or anything special — the function requires two validInts
, so I only need to deal with integers — and I don't think there is any number that cannot be deemed greater than, less than, or equal to another?Is this just GHC assuming I have not covered all cases because I didn't add a final
otherwise =
guard? Even though it logically covers all cases.
Also, if you have any tips on how to write more idiomatic Haskell, I'd appreciate them. I'm still learning the basics.
原文:https://stackoverflow.com/questions/46354999
最满意答案
听起来像是你面临元素属性
value
和value
属性混淆的区别。 这些不是一回事 。问题在于value-attribute服务于默认值的目的,因此如果该元素已经具有属性值,那么更改value-attribute将不会反映在UI中。
文档说这个 :
使用setAttribute()修改某些属性,最显着的是XUL中的值,工作不一致,因为该属性指定了默认值。 要访问或修改当前值,应该使用属性。 例如,使用elt.value而不是elt.setAttribute('value',val)。
所以为了演示这种情况,请考虑这个小小的演示:
document.getElementById("01").getElementsByTagName("input")[0].value = 'property set'; document.getElementById("01").getElementsByTagName("input")[0].setAttribute("value", "two");
<div id="01"> <input type="text" /> </div>
在上面的代码片段中,value属性确实更新为值
two
,如果尝试使用getAttribute('value')
读取它,则可以验证它,但value属性优先于属性,因此以后不会渲染。Sounds like you faced a confusing difference between element property
value
andvalue
attribute. Those are not the same things.The thing is that value-attribute serves the purpose of the default value, so if the element already has a property value then changing value-attribute will not be reflected in UI.
Documentation says this:
Using setAttribute() to modify certain attributes, most notably value in XUL, works inconsistently, as the attribute specifies the default value. To access or modify the current values, you should use the properties. For example, use elt.value instead of elt.setAttribute('value', val).
So to demonstrate this situation consider this little demo:
document.getElementById("01").getElementsByTagName("input")[0].value = 'property set'; document.getElementById("01").getElementsByTagName("input")[0].setAttribute("value", "two");
<div id="01"> <input type="text" /> </div>
In the above snippet the value attribute is indeed updated to value
two
and you can verify it if you try to read it back withgetAttribute('value')
, however the value-property takes precedence over attribute, so later is not rendered.
相关问答
更多-
DotNetNuke:为什么没有“记住我”像我期望的那样工作?(DotNetNuke: Why doesn't “Remember me” work like I expect?)[2023-03-28]
首先,要启用该功能,如orka所说,您需要将web.config中的PersistentCookieTimeout设置为大于0的值(以分钟为单位)。 除此之外,我建议在登录屏幕上改变呈现给用户的言辞以更准确。 “记住登录”确实使它看起来像是要自动填充你的凭证 - 从功能角度来看,这确实没有意义。 所以,我建议将“记住登录”更改为“保持登录状态”。 您可以通过浏览至管理 - >语言 - >(编辑英文站点链接的静态资源) - >本地资源 - >桌面模块 - >管理 - >身份验证 - > App_LocalRe ... -
jQuery使用带有属性名称的toLowerCase,因此最终为gradienttransform而不是gradientTransform ,这是一个新属性,与您已有的属性不同,因此存在问题。 attr()方法就像这样开始 function (elem, name, value) { var hooks, ret, nType = elem.nodeType; // don't get/set attributes on text, comment and attribute nodes ...
-
它是getElementsByClassName ,而不是getElementByClass ; 详情请点击 请注意,IE不支持此功能 (还)。 getElementsByClassName返回匹配元素(而不是单个元素)的NodeList,因此: var list, index; list = document.getElementsByClassName("home1"); for (index = 0; index < list.length; ++index) { list[index].se ...
-
您的broadcast频道是无缓冲的 。 这意味着: 您将消息发送到handle通道:主要goroutine阻止,直到... 在goroutine中, select case获取消息...... 并调用(在goroutine中) handler ,它将消息发送到broadcast频道,阻塞直到...... 就是这样:你的孩子goroutine正在阻止,等待自己选择信息。 同时,你的主要goroutine睡觉,然后到达main的末尾,退出并终止程序。 你可以通过几种方式“解决”它: 使您的broadcast频 ...
-
Lo-Dash - 帮助我理解_.pick为什么不按我期望的方式工作(Lo-Dash - help me understand why _.pick doesn't work the way I expect)[2023-08-12]
http://lodash.com/docs#pick 它期望一个Object作为第一个参数,你给它一个Array 。 Arguments 1. object (Object): The source object. 2. ... 3. ... 我认为这是你能做的最好的事情: MyCollection.prototype.select = function (properties) { var self = this; return { where: function (con ... -
setAttribute的替代方法(alternatives to setAttribute)[2024-01-27]
一般来说,你想要在JavaScript中处理属性而不是属性 。 属性在DOM节点上。 属性使用HTML标记。 有时属性会自动转换为属性。 htmlFor ,用htmlFor代替。 var label = document.createElement("label"); label.htmlFor= "CameraQuality"; Generally you want to deal with properties in JavaScript instead of attributes. Properti ... -
setAttribute()不按我期望的方式工作[复制](setAttribute() doesn't work the way I expect it to [duplicate])[2022-02-03]
听起来像是你面临元素属性value和value属性混淆的区别。 这些不是一回事 。 问题在于value-attribute服务于默认值的目的,因此如果该元素已经具有属性值,那么更改value-attribute将不会反映在UI中。 文档说这个 : 使用setAttribute()修改某些属性,最显着的是XUL中的值,工作不一致,因为该属性指定了默认值。 要访问或修改当前值,应该使用属性。 例如,使用elt.value而不是elt.setAttribute('value',val)。 所以为了演示这种情况,请 ... -
如何使用EasyMock期望(How to use EasyMock expect)[2023-09-30]
您需要在您的模拟对象上调用replay方法,以便它开始返回您配置它的内容。 Okay, after analysing I finally got it to work by adding EasyMock.replay(userBookLimiter); So the setup method looks like this: @Before public void setUp() throws Exception { userBookLimiter = EasyMock.createMock(U ... -
它必须是{% if extend %} - 因为extend已经是布尔值 - 或{% if extend == true %} 。 是用于测试 ; 不是为了比较。 It has to be either {% if extend %} — because extend is already a boolean — or {% if extend == true %}. is is used for tests; not for comparison.
-
由于任何union或struct的开头都将在边界上对齐,因此您不能以32位方式将所有数据拟合。 你应该将你的union和struct ,如下所示(为了便于阅读,删除了属性): typedef struct MyStruct { union { struct { unsigned state : 2; unsigned id : 30; } struct { unsigned /* unused */ : ...