Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The solution is to use an accumulating list in <code>menuRectangles</code>. </p> <p>When you call <code>addRectangles</code> you have to bind the result to a new variable - that is what this expression is doing:</p> <pre><code>rs_new &lt;- addRectangle rs </code></pre> <p>Note - this is special syntax in Haskell for binding within an IO or monadic expression.</p> <p>In Haskell the other binding form is <code>let</code>:</p> <pre><code>let xs = [2,3,4] ys = 1 : xs in length ys </code></pre> <p>Here's a working version of your program - note you now have to run with <code>main</code> so it can seed <code>menuRectangles</code> with the empty list. When <code>menuRectangles</code> is called recursively for a menu option you have to pass on the accumulating list.</p> <pre><code>import IO import Char main :: IO () main = do xs &lt;- menuRectangles [] mapM print xs return () -- A new helper - this will throw an error -- if you don't type a number. There are -- plenty of ways to make it more robust. -- getInt :: IO Int getInt = do xs &lt;- getLine return (read xs) menuRectangles :: [RectangleType] -&gt; IO [RectangleType] menuRectangles rs = do putStrLn "Please choose option:" putStrLn "1 - Add rectangle" putStrLn "2 - Show rectangle" putStrLn "3 - Quit" putStr "Number: " n &lt;- getLine case n of "1" -&gt; do { rs_new &lt;- addRectangle rs; menuRectangles rs_new }; "2" -&gt; do { showRectangle rs; menuRectangles rs } "3" -&gt; do { putStrLn "Quitting"; return rs } otherwise -&gt; do { putStrLn "The End"; return rs } addRectangle :: [RectangleType] -&gt; IO [RectangleType] addRectangle rs = do putStrLn "Id: " id &lt;- getInt putStrLn "Width: " width &lt;- getInt putStrLn "Height: " height &lt;- getInt let new_rs = addingRectangle (Rectangle id width height) rs return new_rs showRectangle :: [RectangleType] -&gt; IO () showRectangle rs = do putStrLn "Choose id rectangle: " id &lt;- getInt -- Need a lookup case findRectangle id rs of Nothing -&gt; putStrLn ("Not found " ++ show id) Just rect1 -&gt; putStrLn (showingRectangle rect1) data RectangleType = Rectangle Int Int Int deriving(Show) addingRectangle :: RectangleType -&gt; [RectangleType] -&gt; [RectangleType] addingRectangle r [] = [r] addingRectangle r rx = r:rx showingRectangle :: RectangleType -&gt; String showingRectangle (Rectangle id width height) = "id: " ++ show id ++ "width: " ++ show width ++ "height: " ++ show height ++ "\n"; -- We have to use Maybe - Just rectangle OR Nothing -- to account for the case where the id does not match -- findRectangle :: Int -&gt; [RectangleType] -&gt; Maybe RectangleType findRectangle _ [] = Nothing findRectangle n ((Rectangle i w h):xs) = if n == i then Just (Rectangle i w h) else findRectangle n xs </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload