Here is the code:
data Tree t = NilT
| Node t (Tree t) (Tree t)
instance Show (Tree t) where
show NilT = ""
show Node t l r = (show t) ++ ", " ++ (show l) ++ ", " ++ (show r)
how to use "show" in "t show" with the default setting and use "show" with the tree data with the definition given by myself?
In order to use show t, you must add the constraint Show t to your instance definition.
instance Show t => Show (Tree t) where
show NilT = ""
show (Node t l r) = show t ++ ", " ++ show l ++ ", " ++ show r
You were also missing parenthesis around your pattern Node t l r, and I removed the parenthesis around the calls to show, as they were redundant since function application already has the highest precedence.
Just a side note: There is a function Data.List.intersperse for putting a value between list elements.
show (Node t l r) = concat $ intersperse ", " [show t, show l, show r]
Or shorter, as hammar pointed out:
show (Node t l r) = intercalate ", " [show t, show l, show r]
Unfortunately you can't write map show [t, l, r], as the list elements need to have a unique type.
Related
I've started programming in haskell like 2 months ago, im fairly new so don't expect me to be some top tier expect at monads or whatever please. I have tried in so many ways to get this Forest dataType be instance of functor and show, but i really don't know how to solve the conflicts that the compiler is giving to me. Such as:
Not in scope: data constructor ‘Tree’
Perhaps you meant ‘True’ (imported from Prelude)
|
15 | show ((Tree a) : (Forest s) ) = "[" ++ show a ++ "," ++ show s ++ "]"
| ^^^^
exercici3.hs:15:23: error: Not in scope: data constructor ‘Forest’
|
15 | show ((Tree a) : (Forest s) ) = "[" ++ show a ++ "," ++ show s ++ "]"
| ^^^^^^
exercici3.hs:19:12: error:
Not in scope: data constructor ‘Tree’
Perhaps you meant ‘True’ (imported from Prelude)
|
19 | fmap ((Tree a) : (Forest s)) = [f a] ++ (fmap f s)
| ^^^^
exercici3.hs:19:23: error: Not in scope: data constructor ‘Forest’
|
19 | fmap ((Tree a) : (Forest s)) = [f a] ++ (fmap f s)
| ^^^^^^
This is the font code of the classes. I've been thinking for a long time and i can't find a resonable solution, all help is welcome , thank you!
data Tree a = Empty | Node a (Tree a) (Tree a)
data Forest a = Nil | Cons (Tree a) (Forest a)
instance Show a => Show (Tree a) where
show Empty = "()"
show (Node b (xl) (xr)) = "(" ++ show xl ++ "," ++ (show b) ++ "," ++ show xr ++ ")"
instance Functor (Tree ) where
fmap f Empty = Empty
fmap f (Node a (xl) (xr)) = Node (f a) (fmap f xl) (fmap f xr)
instance Show a => Show (Forest a) where
show Nil = []
show ((Tree a) : (Forest s) ) = "[" ++ show a ++ "," ++ show s ++ "]"
instance Functor (Forest) where
fmap f Nil = []
fmap ((Tree a) : (Forest s)) = [f a] ++ (fmap f s)
Just to be clear, the Tree datatype works just fine, its just the syntactic part of the forest that doesnt seem to work at all.
The data constructor is Cons, not (:). Then you use for example x and xs as variables, where x has type Tree a, and xs has type Forest a:
instance Show a => Show (Forest a) where
show Nil = ""
show (Cons x xs) = "[" ++ show x ++ "," ++ show xs ++ "]"
instance Functor Forest where
fmap f Nil = Nil
fmap f (Cons x xs) = Cons (fmap f x) (fmap f xs)
That being said, I don't see much reasons to define a data type Forest here, you can define this as:
type Forest a = [Tree a]
I'd like to instance show function for my binary tree, constructed this way: data Tree a = Nil | Leaf a | Branch a (Tree a) (Tree a).
I'd like to achieve a representation like "tree" unix command. For instance:
The showing function would be:
> 27
>> 14
>>> 10
>>> 19
>> 35
>>> 31
>>> 42
I want to tabulate each "subtree" with a recursive function but i don't kwow how this is my actual code:
instance (Show a)=>Show (Tree a) where
show Nil = ""
show (Leaf e) = show e
show (Branch e ls rs) = show e ++ "\n\t" ++ show ls ++ "\n\t" ++ show rs
So the question is: how can i implement a recursive tabulation function, because each time i use new line and tabulate just once instead of subtree depth
You can define a helper function, let's call it showWithDepth like this:
showWithDepth :: (Show a) => Tree a -> Int -> String
showWithDepth Nil _ = ""
showWithDepth (Leaf e) depth = (replicate depth '\t') ++ show e ++ "\n"
showWithDepth (Branch e ls rs) depth = (replicate depth '\t') ++ show e ++ "\n" ++ showWithDepth ls (depth+1) ++ showWithDepth rs (depth+1)
And now we can simply define Your instance like this:
instance (Show a)=>Show (Tree a) where
show x = showWithDepth x 0
I'm trying to apply a function to a Matrix but I don't know how to proceed.
Here's how I define my Matrix :
data Matrice a = Mat [[a]]
montre [] = "/"
montre (t:q) = "" ++ (Pp.printf "%5s" (show t)) ++ " " ++ (montre q)
instance (Show a) => Show (Matrice a) where
show (Mat ([])) = ""
show (Mat (t:q)) = "/" ++ (montre t) ++ "\n" ++ (show (Mat q))
Then, once my Matrix is defined I'd like to apply my function z95 to each of the elements of the matrix.
Here's the signature of my z95 function (which allows to convert a integer into this integer modulo 95)
z95 n = Z95(n %% 95)
z95 18 = 18%95
I tried to do a double map too access the elements of my Matrix but then I didn't figure out how to apply my z95 function.
Thanks fo the help!
You could define a Functor instance for your type, which is the usual way to map a function over the elements of a container.
instance Functor Matrice where
fmap f (Mat xss) = Mat (map (map f) xss)
Now you can write
>> let m = Mat [[1,2,3],[4,5,6]]
>> fmap (+3) m -- => Mat [[4,5,6],[7,8,9]]
or in your case
>> fmap z95 m
I am trying to write a show instance to display well formed formula but after miming whole syntax I am still facing the same error as below.
Hugs> :load "C:\\Users\\Devil\\Desktop\\CASESTUDY1.hs"
ERROR file:.\CASESTUDY1.hs:15 - Ambiguous variable occurrence "show"
*** Could refer to: CASESTUDY1.show Hugs.Prelude.show
Below is the content of my .hs file include data type and related show instance .
module CASESTUDY1
where
data Wff = VAR String
| NEG Wff
| AND Wff Wff
| OR Wff Wff
| IMPL Wff Wff
instance Show Wff where
show (VAR x) = x
show (NEG x) = "~" ++ show(x)
show (AND x y) = "(" ++ show(x) ++ "^" ++ show(y) ++ ")"
show (OR x y) = "(" ++ show(x) ++ "v" ++ show(y) ++ ")"
show (IMPL x y) = "(" ++ show(x) ++ "-->" ++ show(y) ++ ")"
In haskell, whitespace is important. You need to indent the show's that belong to your instance of Show.
instance Show Wff where
show (VAR x) = show x
show (NEG x) = "~" ++ show x
show (AND x y) = "(" ++ show x ++ "^" ++ show y ++ ")"
show (OR x y) = "(" ++ show x ++ "v" ++ show y ++ ")"
show (IMPL x y) = "(" ++ show x ++ "-->" ++ show y ++ ")"
Also, you do not need parenthesis to pass the parameters to show. show(x) should be show x.
If you are learning haskell I recommend these exceptional resources:
Learn You a Haskell For Great Good
Real World Haskell
I am new at haskell and I want to instance Tree a with the class show.
data Tree a = Null
|Node (Tree a) a (Tree a)
instance Show (Tree a) where
show Null = ""
show Node ((Tree l) (v) (Tree r)) = "|"--I don´t know how i can do this step
Thanks for your help.
Apply show recursively:
data Tree a = Null | Node (Tree a) a (Tree a)
instance Show a => Show (Tree a) where
show Null = "Null"
show (Node l v r) = "(" ++ show l ++ " " ++ show v ++ " " ++ show r ++ ")"