reset password
Author Message
jungsoolim
Posts: 38
Posted 20:24 Feb 16, 2019 |

I started with creating a stack as this:

data IntString = I Integer | S String | C Char deriving Show
data List l = List [IntString]


--data Stack a = Stack [a] deriving Show


--push :: IntString -> Stack IntString -> Stack IntString
--push x (Stack xs)= Stack (x:xs)


--pop :: Stack a -> (a, Stack a)
--pop (Stack []) = (Nothing, Stack [])
--pop (Stack (x:xs)) = (Just x, Stack xs)

However, Professor Abbott suggests to use a list in place of stack as

ghci> let l = List
ghci> l = [(I 5), (S "abc"), (C '+')]
ghci> l
[I 5,S "abc",C '+']
ghci> 

This is much simpler and easier to work with.

 

 

 

rabbott
Posts: 1649
Posted 20:40 Feb 16, 2019 |

I don't think you have to declare a List data type. Just use a list directly as in your example. (You can also get along without the parentheses.)

ghci> data IntString = I Integer | S String | C Char deriving Show
ghci> let x = [I 5, S "abc", C '+']
ghci> x
[I 5,S "abc",C '+']
ghci> :type x
x :: [IntString]


 

Last edited by rabbott at 20:40 Feb 16, 2019.
rabbott
Posts: 1649
Posted 20:48 Feb 16, 2019 |

For the particular problem this week, though, since there only character is '+' and the only string is 'fib' just make them constructors.

> data FibElement = I Integer | Fib | Plus deriving Show

> x = [Plus, Fib, I 5, Fib, I 4]

> x
[Plus,Fib,I 5,Fib,I 4]

> :type x
x :: [FibElement]

 

jungsoolim
Posts: 38
Posted 20:50 Feb 16, 2019 |

Thanks! 

rabbott
Posts: 1649
Posted 20:56 Feb 16, 2019 |

Perhaps even better, since you will never be applying Fib to an unknown argument (that is, whenever you put Fib into a list, you know at that time what its argument is), you might define:

 

> data FibElement = I Integer | Fib Integer | Plus deriving Show

> x = [Plus, Plus, Fib 5, Fib 4, I 3]

> x
[Plus,Plus,Fib 5,Fib 4,I 3]

> :type x
x :: [FibElement]

 

Last edited by rabbott at 21:00 Feb 16, 2019.