【发布时间】:2021-02-01 01:58:40
【问题描述】:
所以我正在做一些coding problem 并且为了解决它,我正在尝试创建所有可能答案的列表,然后我会在被要求时查看该答案是否存在。但是,我遇到了函数优先级“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]
【问题讨论】:
-
一点风格建议:你可以把
answerList改写成answerList = createOptions <$> [0..3] <*> [0..3] <*> [0..3] -
对我来说听起来像是 XY 问题。大概您想将
4+4+(4*4)和(4+4+4)*4(以及其他...4+(4+4)*4等)都视为潜在候选人。如果是这样,您真的只需要在内部节点上使用这些操作枚举树,然后在不考虑优先级的情况下评估树。我们只使用优先级和括号来为表达式获得一种方便的扁平语法,但结构化的树表示才是表达式的真正基础。
标签: haskell operator-precedence