Giter Site home page Giter Site logo

hw05's Introduction

MI-AFP homework #05

Homework to practice advanced work with typeclasses

Task

  1. You've already done some implementation of area and circumference for various shapes in the third homework. Now you will do something similar, but using typeclasses in Data.Shapes!
    • First, implement instances of Validable for each shape type with function valid that says if given shape is valid (its attributes are acceptable, i.e., not negative, forming a triangle).
    • Then, design on your own the Shape2D typeclass which is a subclass of Validable, define area and circumference (remove dummy ones). During the design consider using valid - for invalid shape, both functions should return zero.
    • Finally, implement instances of Shape2D for each shape type.
  2. Data.SortedList = implement all necessary functions to make data type SortedList to be an instance of Semigroup, Monoid, Functor, Applicative, Monad. After applying (<>) the resulting list must be sorted. Sadly, Functor, Applicative, and Monad won't allow you to use Ord constaint so the result won't be sorted. Implement some sort function if you want to... It is forbidden to just delegate to list and use fromList and toList.
  3. The final (and also the most interesting) task is to implement functions and instances for next data type to represent integers - the RomanNumeral (from Data.RomanNumeral)! It is a representation of an integer as Roman numeral, for example, 125 is "CXXV".
    • The biggest challenge is to implement functions integral2RomanNumeral and romanNumeral2Integral (related with pack and unpack). There is something already prepared in Data.RomanNumeral.Helpers. Take a look and feel free to edit as you need.
    • After having pack and unpack working, it will be very easy to make instances of Bounded, Eq, Ord, Num, Enum, and Integral (you might need some more due to dependencies) for the RomanNumeral.

Hints & general requirements:

  • You should not hardcode any string or character in Data.RomanNumeral - use only those defined in Data.RomanNumeral.Helpers or enhance it.
  • Being DRY is essential, do not repeat code. Name expressions to reuse then, introduce helper functions.
  • Local names (via where or let-in) in functions should be introduced to make the code more readable. Creating very simple helper functions in module scope is not nice. If you need some complicated functions create separate "private" module.
  • Make your code as clean as possible. Prefer pattern matching and syntactic sugar everywhere it is reasonable.
  • You must understand your code completely!

Notes

  • If you encounter some trouble, create an issue in your repository.
  • In case you find a bug or have an idea how to improve assignment project, create an issue or PR in this repository.
  • It might happen that tests are not 100% bulletproof, but your solution should be correct. If you want, propose tests enhancements.

License

This project is licensed under the MIT License - see the LICENSE file for more details.

hw05's People

Contributors

mareksuchanek avatar

Watchers

 avatar  avatar  avatar

hw05's Issues

Problematic definition of modulo arithmetic

Hi! ๐Ÿ™‚ I have looked on your fixed version of tests and I would like you to specify the definition of your modulo arithmetic.

As far as I have been taught, the modulo of n must be in range from 0 to n - 1. However, according to your tests, this does not hold - see

(RomanNumeral "-IV" quotRem RomanNumeral "III") shouldBe (RomanNumeral "-I", RomanNumeral "-I") ~ (-4 mod 3 = -1) ~ I would say it should be 2.

Also I doubt anyone in any of our math courses have even used a negative number as base for modulo operation, but then I would say the result should be in range from n + 1 to 0. However, the tests state following

(RomanNumeral "X" quotRem RomanNumeral "-III") shouldBe (RomanNumeral "-III", RomanNumeral "I") ~ (10 mod -3 = 1) ~ I (and even Prelude) say that it should be -2.

Could you please clarify this? Thank you ๐Ÿ™‚

Copy-Paste typo in test

it "matches with AnyInternal" $ do
  (AnyInternal ~~ esInternal1) `shouldBe` True
  (AnyInternal ~~ esExternal)  `shouldBe` False
  (AnyInternal ~~ esCombined)  `shouldBe` True
  (AnyInternal ~~ Unknown)     `shouldBe` False
  (AnyInternal ~~ Combined [esExternal,Unknown]) `shouldBe` False
it "matches with AnyInternal" $ do
  (AnyExternal ~~ esInternal1) `shouldBe` False
  (AnyExternal ~~ esExternal)  `shouldBe` True
  (AnyExternal ~~ esCombined)  `shouldBe` True
  (AnyExternal ~~ Unknown)     `shouldBe` False
  (AnyExternal ~~ Combined [esInternal1,Unknown]) `shouldBe` False
it "matches with AnyInternal" $ do
  (Any ~~ esInternal1) `shouldBe` True
  (Any ~~ esExternal)  `shouldBe` True
  (Any ~~ esCombined)  `shouldBe` True
  (Any ~~ Unknown)     `shouldBe` True
  (Any ~~ Combined [esInternal1,Unknown]) `shouldBe` True

copy-paste typos in titles

Incorrect values in test suite for roman numbers

Hi! ๐Ÿ™‚ I think I have found several mistakes in the test suite for RomanNumeral in HW-05. I will post my findings bellow as well as create PR with the proposed fix.

  • unpack (RomanNumeral "D") shouldBe 100 -> should be 500

  • unpack (RomanNumeral "-CCXV") shouldBe 215 -> should be (-215)

  • unpack (RomanNumeral "") shouldBe 0
    ~ I believe this is correct

  • evaluate (unpack (RomanNumeral "")) shouldThrow errorCall "Illegal Roman Numeral: ''"
    ~ I believe this is not

  • (RomanNumeral "MCCXL" + RomanNumeral "-MM") shouldBe RomanNumeral "DCCLX"
    ~ I believe that 1240 - 2000 = -760 and not 760 as stated in test -> therefore should be "-CCLX"

  • (RomanNumeral "MCCXL" - RomanNumeral "MM") shouldBe RomanNumeral "DCCLX"
    ~ From same reasons as above I believe this should be "-DCCLX"

  • (RomanNumeral "" - RomanNumeral "VII") shouldBe RomanNumeral "VII"
    ~ Similar reasons, this should be 0 - 7 = -7 and therefore "-VII"

  • All tests in "is Integral: can compute quotient and remainder" attempt to divide by zero, which results in an exception being thrown (it seems author forgot to add in values)

  • toInteger (RomanNumeral "-DCCLXXXV") shouldBe 785 is once again negative number and therefore the result should be (-785)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.