Difference between revisions of "FP Laboratory 11"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 1: Line 1:
 +
<translate>
 +
== Complex data structure ==
 +
Consider following data structure representing some kind of GUI.
 +
</translate>
 +
 +
<syntaxhighlight lang="Haskell">
 +
data Point = Point {column::Int,row::Int} deriving (Show)
 +
 +
data Event = MouseEvent Point
 +
          | KeyEvent {keyPressed::Char} deriving (Show)
 +
 +
data Position = Position {leftTopCorner :: Point, width :: Int, height :: Int}
 +
 +
data Component
 +
  = TextBox {name :: String, position :: Position, text :: String}
 +
  | Button {name :: String, position :: Position, text :: String, onClick :: Maybe ((Event, Component) -> String)}
 +
  | Container {name :: String, children :: [Component]}
 +
</syntaxhighlight>
 +
 +
<translate>
 +
As an example, we can use following data structure.
 +
</translate>
 +
 +
<syntaxhighlight lang="Haskell">
 +
gui :: Component
 +
gui =
 +
  Container
 +
    "My App"
 +
    [ Container
 +
        "Menu"
 +
        [ Button "btn_new" (Position (Point 0 0) 100 20) "New" Nothing,
 +
          Button "btn_open" (Position (Point 100 0) 100 20) "Open" Nothing,
 +
          Button "btn_close" (Position (Point 200 0) 100 20) "Close" Nothing
 +
        ],
 +
      Container "Body" [TextBox "textbox_1" (Position (Point 0 20) 300 500) "Some text goes here"],
 +
      Container "Footer" []
 +
    ]
 +
</syntaxhighlight>
 +
 
<translate>
 
<translate>
 
== Additional exercises ==
 
== Additional exercises ==

Revision as of 12:00, 15 November 2023

Complex data structure

Consider following data structure representing some kind of GUI.

data Point = Point {column::Int,row::Int} deriving (Show)

data Event = MouseEvent Point
           | KeyEvent {keyPressed::Char} deriving (Show)

data Position = Position {leftTopCorner :: Point, width :: Int, height :: Int} 

data Component
  = TextBox {name :: String, position :: Position, text :: String}
  | Button {name :: String, position :: Position, text :: String, onClick :: Maybe ((Event, Component) -> String)}
  | Container {name :: String, children :: [Component]}

As an example, we can use following data structure.

gui :: Component
gui =
  Container
    "My App"
    [ Container
        "Menu"
        [ Button "btn_new" (Position (Point 0 0) 100 20) "New" Nothing,
          Button "btn_open" (Position (Point 100 0) 100 20) "Open" Nothing,
          Button "btn_close" (Position (Point 200 0) 100 20) "Close" Nothing
        ],
      Container "Body" [TextBox "textbox_1" (Position (Point 0 20) 300 500) "Some text goes here"],
      Container "Footer" []
    ]

Additional exercises

  • Consider the following definition and the example of the m-ary tree.
data MTree a = MTree a [MTree a]
testTree1 :: MTree Int            
testTree1 = MTree 1 [(MTree 2 [(MTree 3 []),(MTree 4 [(MTree 5 []),(MTree 6 [])]), (MTree 7 []),(MTree 8 [])]), (MTree 9 [])]
  • Create a function that sums all values stored in the m-ary tree.
msum :: MTree Int -> Int
  • Create a function that extracts all values from the m-ary tree into a list.
mToList :: MTree a -> [a]
  • Create a function that counts all leaves in the m-ary tree.
mLeafCount :: MTree a -> Int
  • Create a function that finds a maximum value stored in the m-ary tree.
mMaxTree :: Ord a => MTree a -> a
  • Create a function that checks whether a given element is stored in the m-ary tree.
mContains :: Eq a => MTree a -> a -> Bool
  • Create a function that returns a number of elements greater than a given value.
mGreaterThan :: Ord a => MTree a -> a -> Int