0

enter image description here

I would like to implement the DPLL algorithm above in Haskell. But the problem is I don't know how to get multiple if statements to run. So I was thinking, you can pattern match for the first 2 if statements. But you can't do it for the third and fourth? Because both of them must run and the return statement must run too.

How do I make multiple if statements like the above in Haskell? Also I'm quite new to Haskell so I won't be able to do anything 'complicated'.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
karambit
  • 167
  • 1
  • 10
  • 1
    If you are "quite new to Haskell" and you "won't be able to do anything complicated" maybe writing a DPLL SAT solver is not the most appropriate choice for a first exercise. By all means, give it a try, but it could be helpful to get familiar with the language solving some more basic task, at first. – chi Feb 13 '16 at 23:14

1 Answers1

4

Use pattern guards. For example

dpll clauses symbols modell
  | "all clauses true" = true
  | "some clauses false" = false
  | (p,value) <- find_pure_symbol symbols clauses model,
    nonnull p = dpll clauses ...
  | (p,value) <- find_unit_clause clauses model,
    nonnull p = dpll clauses ...
  | p <- first symbols, r <- rest symbols =
                dpll clauses ... || dpll clauses ....

(It looks like not all clauses true does not mean some clauses false, otherwise you could never reach the 3rd and following cases.)

The challenge is then to formulate the conditions, in the example I have marked them with " around them, but they are normal haskell expressions of type Bool.

Ingo
  • 36,037
  • 5
  • 53
  • 100
  • Is this really correct? I don't see how your code would let the last 2 ifs to run? – karambit Feb 13 '16 at 23:15
  • @karambit I assumed that you have functions like `find_pure_symbol` and `find_unit_symbol` that return a tuple `(p, value)`. The pattern guard clause with the `<-` applies the function, and the following condition (separated with a comma) is your *if*. I also assumed the existence of the functions `nonnull`, `rest` and `first` – Ingo Feb 13 '16 at 23:23