首页 \ 问答 \ 非穷举模式匹配只是因为我遗漏了`否则=`?(Non-exhaustive pattern matches only because I left off `otherwise =`? [duplicate])

非穷举模式匹配只是因为我遗漏了`否则=`?(Non-exhaustive pattern matches only because I left off `otherwise =`? [duplicate])

这个问题在这里已经有了答案:

我在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 the respond function. What I don't understand is which case I haven't covered. Those aren't Maybe Ints or anything special — the function requires two valid Ints, 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
更新时间:2021-09-28 07:09

最满意答案

听起来像是你面临元素属性valuevalue属性混淆的区别。 这些不是一回事

问题在于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 and value 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 with getAttribute('value'), however the value-property takes precedence over attribute, so later is not rendered.

相关问答

更多
  • 首先,要启用该功能,如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频 ...
  • 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 ...
  • 一般来说,你想要在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 ...
  • 听起来像是你面临元素属性value和value属性混淆的区别。 这些不是一回事 。 问题在于value-attribute服务于默认值的目的,因此如果该元素已经具有属性值,那么更改value-attribute将不会反映在UI中。 文档说这个 : 使用setAttribute()修改某些属性,最显着的是XUL中的值,工作不一致,因为该属性指定了默认值。 要访问或修改当前值,应该使用属性。 例如,使用elt.value而不是elt.setAttribute('value',val)。 所以为了演示这种情况,请 ...
  • 您需要在您的模拟对象上调用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 */ : ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)