Reputation: 127
i do not know where the empty tuples coming from ? since index is so big, i can not see the content of result
how to remove empty tuples to make it filter to print out all true result?
error:
*Main> let bb = filter (\n -> snd n == True) alltrees1
:74:39:
Couldn't match expected type (a0, Bool)' with actual type
()'
Expected type: [(a0, Bool)]
Actual type: [()]
In the second argument of filter', namely
alltrees1'
In the expression: filter (\ n -> snd n == True) alltrees1
run:
:l trees.hs
let allparams = replicateM 3 [1.0, 2.0]
let alltrees = [getAllTrees c | x <- allparams, c <- [x]]
eval(alltrees!!1!!1) == eval(alltrees!!1!!1)
let alltrees1 = forM_ [0..(sizeoflist alltrees)] $ \i -> forM_ [0..(sizeoflist (alltrees!!i))] $ \j -> [(alltrees!!i!!j, eval(alltrees!!i!!j) == eval(alltrees!!i!!j))]
let bb = filter (\n -> if n != () then snd n == True) alltrees1
haskell:
import Data.List
import Control.Monad
import Math.Combinat
import System.IO
data Operation
= And
| Or
| MA
| MB
| Impl
deriving Show
data Mree x
= Meaf x
| Mode (Mree x) Operation (Mree x)
deriving Show
splits :: [a] -> [([a], [a])]
splits xs = zip (inits xs) (tails xs)
getAllTrees :: [a] -> [Mree a]
getAllTrees [] = []
getAllTrees [x] = return $ Meaf x
getAllTrees xs = do
(left, right) <- splits xs
guard $ not (null left) && not (null right)
leftT <- getAllTrees left
rightT <- getAllTrees right
op <- [And, Or]
return $ Mode leftT op rightT
eval :: Mree Double -> Double
eval (Meaf x) = x
eval (Mode l And r) = eval l
eval (Mode l Or r) = eval r
sizeoflist :: [a] -> Int
sizeoflist = length
Upvotes: 0
Views: 308
Reputation: 571
The basic haskell List (and most collections) won't let you store empty tuples and non-empty tuples in the same list, because they are different types—so, in this case, it doesn't make sense to say
\n -> if n != () then snd n == True
Now: why is the list a list of empty tuples? Because of the type of forM_ (found via hoogle):
forM_ :: (Foldable t, Monad m) => t a -> (a -> m b) -> m ()
In the context above (iterating over a list, producing a list), this is specifically:
forM_ :: [a] -> (a -> [b]) -> [()]
The return value from forM_ (and, as a naming convention, many functions ending with underscore, like traverse_, for_, and sequenceA_) is "useless"; these functions are typically used to execute side effects in some monad like IO.
Note: since you aren't using those indices for anything but to visit each list member, it'd be a little cleaner to just use map:
map (map (\x -> (x, eval x == eval x))) allTrees
For more info about lists, see the chapter from the book learn you a haskell.
Upvotes: 0
Reputation: 52029
Note that the type of altrees1
is [()]
. By using forM_
you are discarding the results of inner computations.
Note that the type of altrees
is [ [ Mree Double ] ]
.
Perhaps this is what you want:
allexprs :: [ Mree Double ]
allexprs = concat alltrees -- a list of all the Mrees in alters
alltrees2 = [ (x, eval x == eval x) | x <- allexprs ]
-- this is apparently what alltrees1 is doing
bb = [ x | (x, True) <- alltrees2 ]
-- same as: filter (\(x,b) -> b == True) altrees2
-- filter (\(x,b) -> b) alltrees2
-- filter (\xb -> snd xb) alltrees2
Upvotes: 1