首页 > 解决方案 > `>||<` 运算符在 Haskell 中的作用是什么?

问题描述

有谁知道'>||<'在第 7 行以下的 Haskell 代码中是什么?(DPLL 算法)

dpll :: Eq a => CNF a -> Valuation a -> Valuation a
dpll e v
| e == [] = v
| elem [] e = []
| units /= [] = dpll (propagate unitLit e) (v ++ [unitLit])
| otherwise = dpll (propagate lit e) (v ++ [lit])
>||< dpll (propagate (neg lit) e) (v ++ [(neg lit)])
where
units = filter (\x -> (length x) == 1) e
unitLit = head $ head units
lit = head $ head e
propagate n e = map (\\ [neg n]) $ filter (notElem n) e
(>||<) x y = if x /= [] then x else y

标签: haskell

解决方案


In haskell, functions can be named with symbols if they are closed by parentheses. For example

(+-+) :: Num a => a -> a -> a -> a
(+-+) x y z = (x+y) - z

it can be used:

(+-+) 3 4 2
=> 5

When you use a where clause, you are telling Haskell to create a function with the scope of the function where is called, so, for example:

fun1 r m = doSome r m
  where
  doSome r1 m1 = r1 + m1

here, doSome function can be called only in fun1 scope, and it takes two numbers and sum them together. In your example:

dpll e v
  where
  units = ...
  unitLit =  ...
  lit = ...
  propagate n e = ...
  (>||<) x y = if x /= [] then x else y

your functions has the type:

(>||<) :: Eq a => [a] -> [a] -> [a]

(>||<) its a function defined in dpll scope, and it returns the first list if the first list is not empty, if the first list is empty, it return the second list.


推荐阅读