TKFALS
TKFALS

Reputation: 89

Help with Haskell

I need some help if possible with the following problem....

  1. atomsClause :: Clause ! [Atom] This function must take a Clause and return the set of Atoms of that Clause. Note that the set should not contain any duplicates.
  2. atoms :: Formula![Atom] This function must take a Formula return the set of Atoms of a Formula. Note that the set should not contain any duplicates.
  3. isLiteral :: Literal ! Clause ! Bool This function returns True if the given Literal can be found within the Clause.
  4. flipSymbol :: Model ! Atom ! Model This function must take a Model and an Atom and flip the truth value of the atom in the model.

Here is the code:

module Algorithm where

import System.Random
import Data.Maybe
import Data.List

type Atom = String
type Literal = (Bool,Atom)
type Clause = [Literal]
type Formula = [Clause]
type Model = [(Atom, Bool)]
type Node = (Formula, ([Atom], Model))

atomsClause :: Clause -> [Atom]
atomsClause = undefined

atoms :: Formula -> [Atom]
atoms = undefined

isLiteral :: Literal -> Clause -> Bool
isLiteral = undefined

flipSymbol :: Model -> Atom -> Model
flipSymbol = undefined

Thank you.

I was thinking for the first one to write it like this......

atomsClause :: Clause -> [Atom]
atomsClause [(Bool,Atom)] =[a|(b,a)<-(Bool,Atom)]

...is this ok?

Upvotes: 0

Views: 466

Answers (1)

fuz
fuz

Reputation: 93172

Your function is not right.

The pattern you match is simply a singleton list. You actually want to match the whole list to work with it. It could look like this:

atomsClause clauses = [a|(b,a)<-clauses]

What does this function does? Let's see... it takes the atom of each clause but there's no filtering. You can archive what you want using the nub function of Data.List. So your function looks like this:

atomsClause clauses = nub [a|(b,a)<-clauses]

Now, we can get rid of the comprehension using a map. We just map the function snd onto clauses:

atomsClause clauses = nub (map snd clauses)

And that's it! If you like it short, you can use this pointless function instead:

atomsClause = nub . map snd

Upvotes: 1

Related Questions