haskell - Haskell 中的函数优先级
问题描述
所以我正在做一些编码问题,为了解决它,我试图创建一个所有可能答案的列表,然后我会在被要求时查看该答案是否存在。但是,我遇到了函数优先级“o1”、“o2”和“o3”的问题——即使它们代表*、div、+等表达式——它们都有相同的优先级,所以当像 4 这样的选项时+ 4 + 4 * 4 出现,程序回答 48 而不是正确答案,即 24。
我想问的是,有什么方法可以改变函数“o1”、“o2”和“o3”的优先级,或者让它们反映运算符的优先级?
编码:
options :: [Int -> Int -> Int]
options = [(+), (-), div, (*)]
optionsStr = [" + 4", " - 4", " / 4", " * 4"]
createOptions :: Int -> Int -> Int -> (Int, String)
createOptions first second third = (key, value)
where
o1 = options !! first
o2 = options !! second
o3 = options !! third
key = 4 `o1` 4 `o2` 4 `o3` 4 -- precedence issue is here
value = "4" ++ (optionsStr !! first) ++ (optionsStr !! second) ++ (optionsStr !! third)
answerList :: [(Int, String)]
answerList = (concat . concat) $ map f [0..3]
where
f x = map (f' x) [0..3]
f' x y = map (createOptions x y) [0..3]
解决方案
推荐阅读
- gekko - GEKKO优化器@error:不等式定义无效的不等式:z > x < y
- python-3.x - Pytorch、INPUT(正常张量)和 WEIGHT(cuda 张量)不匹配
- python - 绑定 C 函数 Python 时的问题
- python - Kivy - 使用实现的 ScrollView 在 GridLayout 中更改 FloatLayout 的行高
- javascript - 反应更新 setState 将字符串转换为数组
- php - PHP 返回一个对象的实例
- excel - VBA 设置页面设置
- javascript - 更改元素的内部文本是否会干扰其子元素?(音频元素的问题)
- javascript - 如何在 .mjs 脚本中使用 Node Sass 解析别名?
- python - Django - 在其他模型 django 管理视图中显示 OneToOneField 字段