[英]What does the `>||<` operator do in Haskell?
Does anyone know what '>||<'
does in the Haskell piece of code that's below-7th line?有谁知道'>||<'
在第 7 行以下的 Haskell 代码段中做了什么? (the DPLL algorithm) (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
In haskell, functions can be named with symbols if they are closed by parentheses.在 haskell 中,如果函数用括号括起来,则可以用符号命名。 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: 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.这里, doSome
function 只能在fun1
scope 中调用,取两个数相加。 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. (>||<)
dpll scope 中定义的dpll
,如果第一个列表不为空,则返回第一个列表,如果第一个列表为空,则返回第二个列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.