Functional MVC

How does the model-view-controller idea translate into functional programming?

In strongly-typed programming languages in particular, the MVC pattern can be more clearly identified in the code than usual. This is because we can “strongly” encode our model with types and define the view and controller simply as functions on the model type. Pure functions and user-defined types get us a clear distinction between the three components of Model-View-Controller!

To define the model, the structure which is updated thorought the program, we simply define a new datatype that models the application to be updated and displayed. Our model will be just company itself!

-- | Our model
data Company = Company Name [Employee]

data Employee = Employee Name Address Salary

The view is just a function from the model to the graphics to be drawn. With Technology:Gloss, the graphics to be displayed are defined with the Picture type. That means the view is the following function with type signature:

view :: Company -> Picture

A controller is modeled by one (or more) function that updates the state of the world. These functions might be called when, for example, events occur, or time passes (such as mouse clicks, or every frame). As an example, in Technology:Gloss, the controller is defined with two separate functions: one that's called every frame, and the other when a user event occurs.


romes edited this article at Sat, 06 Aug 2022 13:41:40 +0200
Compare revisions Compare revisions

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.