diff --git a/haskell/hello-world/README.md b/haskell/hello-world/README.md new file mode 100644 index 0000000000000000000000000000000000000000..aaf5c20fe4cf8fbfee339a10acdc31272b8bbedd --- /dev/null +++ b/haskell/hello-world/README.md @@ -0,0 +1,83 @@ +# Hello World + +The classical introductory exercise. Just say "Hello, World!". + +["Hello, World!"](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) is +the traditional first program for beginning programming in a new language +or environment. + +The objectives are simple: + +- Write a function that returns the string "Hello, World!". +- Run the test suite and make sure that it succeeds. +- Submit your solution and check it at the website. + +If everything goes well, you will be ready to fetch your first real exercise. + +## Hints + +To complete this exercise, you need to implement the `hello` function. + +You will find the type signature for `hello` already in place, +but it is up to you to define the function. + + + +## Getting Started + +Please refer to the [installation](https://exercism.io/tracks/haskell/installation) +and [learning](https://exercism.io/tracks/haskell/learning) help pages. + +## Running the tests + +To run the test suite, execute the following command: + +```bash +stack test +``` + +#### If you get an error message like this... + +``` +No .cabal file found in directory +``` + +You are probably running an old stack version and need +to upgrade it. + +#### Otherwise, if you get an error message like this... + +``` +No compiler found, expected minor version match with... +Try running "stack setup" to install the correct GHC... +``` + +Just do as it says and it will download and install +the correct compiler version: + +```bash +stack setup +``` + +## Running *GHCi* + +If you want to play with your solution in GHCi, just run the command: + +```bash +stack ghci +``` + +## Feedback, Issues, Pull Requests + +The [exercism/haskell](https://github.com/exercism/haskell) repository on +GitHub is the home for all of the Haskell exercises. + +If you have feedback about an exercise, or want to help implementing a new +one, head over there and create an issue. We'll do our best to help you! + +## Source + +This is an exercise to introduce users to using Exercism [http://en.wikipedia.org/wiki/%22Hello,_world!%22_program](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/haskell/hello-world/hello-world.cabal b/haskell/hello-world/hello-world.cabal new file mode 100644 index 0000000000000000000000000000000000000000..efa305abfc7891188772d414fabc7c268190fab1 --- /dev/null +++ b/haskell/hello-world/hello-world.cabal @@ -0,0 +1,36 @@ +cabal-version: 1.12 + +-- This file has been generated from package.yaml by hpack version 0.31.2. +-- +-- see: https://github.com/sol/hpack +-- +-- hash: 7239b8d1fd50ebc9412accddb5ef13cbd655dcca3bf4e413e9822303f1d2e2ae + +name: hello-world +version: 1.1.0.5 +build-type: Simple + +library + exposed-modules: + HelloWorld + other-modules: + Paths_hello_world + hs-source-dirs: + src + ghc-options: -Wall + build-depends: + base + default-language: Haskell2010 + +test-suite test + type: exitcode-stdio-1.0 + main-is: Tests.hs + other-modules: + Paths_hello_world + hs-source-dirs: + test + build-depends: + base + , hello-world + , hspec + default-language: Haskell2010 diff --git a/haskell/hello-world/package.yaml b/haskell/hello-world/package.yaml new file mode 100644 index 0000000000000000000000000000000000000000..20feca01a43f13e9a3403b55683d2691c35552d5 --- /dev/null +++ b/haskell/hello-world/package.yaml @@ -0,0 +1,21 @@ +name: hello-world +version: 1.1.0.5 + +dependencies: + - base + +library: + exposed-modules: HelloWorld + source-dirs: src + ghc-options: -Wall + # dependencies: + # - foo # List here the packages you + # - bar # want to use in your solution. + +tests: + test: + main: Tests.hs + source-dirs: test + dependencies: + - hello-world + - hspec diff --git a/haskell/hello-world/src/HelloWorld.hs b/haskell/hello-world/src/HelloWorld.hs new file mode 100644 index 0000000000000000000000000000000000000000..3dcb71144ee03578426826409a614b80d856d9af --- /dev/null +++ b/haskell/hello-world/src/HelloWorld.hs @@ -0,0 +1,4 @@ +module HelloWorld (hello) where + +hello :: String +hello = "Hello, World!" diff --git a/haskell/hello-world/stack.yaml b/haskell/hello-world/stack.yaml new file mode 100644 index 0000000000000000000000000000000000000000..616941a41618158678b8290988bd44519442b2a5 --- /dev/null +++ b/haskell/hello-world/stack.yaml @@ -0,0 +1 @@ +resolver: lts-14.7 diff --git a/haskell/hello-world/test/Tests.hs b/haskell/hello-world/test/Tests.hs new file mode 100644 index 0000000000000000000000000000000000000000..531f54d1bbf391ae491a56eac2d254fc6fa90556 --- /dev/null +++ b/haskell/hello-world/test/Tests.hs @@ -0,0 +1,11 @@ +import Test.Hspec (Spec, it, shouldBe) +import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith) + +import HelloWorld (hello) + +main :: IO () +main = hspecWith defaultConfig {configFastFail = True} specs + +specs :: Spec +specs = it "hello" $ + hello `shouldBe` "Hello, World!" diff --git a/haskell/leap/README.md b/haskell/leap/README.md new file mode 100644 index 0000000000000000000000000000000000000000..61497f894accfe5fc5dc52dbfec3bf69d858890e --- /dev/null +++ b/haskell/leap/README.md @@ -0,0 +1,95 @@ +# Leap + +Given a year, report if it is a leap year. + +The tricky thing here is that a leap year in the Gregorian calendar occurs: + +```text +on every year that is evenly divisible by 4 + except every year that is evenly divisible by 100 + unless the year is also evenly divisible by 400 +``` + +For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap +year, but 2000 is. + +## Notes + +Though our exercise adopts some very simple rules, there is more to +learn! + +For a delightful, four minute explanation of the whole leap year +phenomenon, go watch [this youtube video][video]. + +[video]: http://www.youtube.com/watch?v=xX96xng7sAE + +## Hints + +To complete this exercise you need to implement the function `isLeapYear` +that takes a year and determines whether it is a leap year. To solve this +exercise you may read up on: + +- [Guards][guards] + +[guards]: https://www.futurelearn.com/courses/functional-programming-haskell/0/steps/27226 + + + +## Getting Started + +Please refer to the [installation](https://exercism.io/tracks/haskell/installation) +and [learning](https://exercism.io/tracks/haskell/learning) help pages. + +## Running the tests + +To run the test suite, execute the following command: + +```bash +stack test +``` + +#### If you get an error message like this... + +``` +No .cabal file found in directory +``` + +You are probably running an old stack version and need +to upgrade it. + +#### Otherwise, if you get an error message like this... + +``` +No compiler found, expected minor version match with... +Try running "stack setup" to install the correct GHC... +``` + +Just do as it says and it will download and install +the correct compiler version: + +```bash +stack setup +``` + +## Running *GHCi* + +If you want to play with your solution in GHCi, just run the command: + +```bash +stack ghci +``` + +## Feedback, Issues, Pull Requests + +The [exercism/haskell](https://github.com/exercism/haskell) repository on +GitHub is the home for all of the Haskell exercises. + +If you have feedback about an exercise, or want to help implementing a new +one, head over there and create an issue. We'll do our best to help you! + +## Source + +JavaRanch Cattle Drive, exercise 3 [http://www.javaranch.com/leap.jsp](http://www.javaranch.com/leap.jsp) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/haskell/leap/leap.cabal b/haskell/leap/leap.cabal new file mode 100644 index 0000000000000000000000000000000000000000..2a72374213e6a48c70e0cdfb8dd73b6e37be7084 --- /dev/null +++ b/haskell/leap/leap.cabal @@ -0,0 +1,36 @@ +cabal-version: 1.12 + +-- This file has been generated from package.yaml by hpack version 0.31.2. +-- +-- see: https://github.com/sol/hpack +-- +-- hash: 2023dceec3fe5a747e981d5a0d4cfc6a111057404efa2ab8be9fe514b6c3a4b7 + +name: leap +version: 1.6.0.10 +build-type: Simple + +library + exposed-modules: + LeapYear + other-modules: + Paths_leap + hs-source-dirs: + src + ghc-options: -Wall + build-depends: + base + default-language: Haskell2010 + +test-suite test + type: exitcode-stdio-1.0 + main-is: Tests.hs + other-modules: + Paths_leap + hs-source-dirs: + test + build-depends: + base + , hspec + , leap + default-language: Haskell2010 diff --git a/haskell/leap/package.yaml b/haskell/leap/package.yaml new file mode 100644 index 0000000000000000000000000000000000000000..5364801aa7d77606a6ec7046c3557320299786b1 --- /dev/null +++ b/haskell/leap/package.yaml @@ -0,0 +1,21 @@ +name: leap +version: 1.6.0.10 + +dependencies: + - base + +library: + exposed-modules: LeapYear + source-dirs: src + ghc-options: -Wall + # dependencies: + # - foo # List here the packages you + # - bar # want to use in your solution. + +tests: + test: + main: Tests.hs + source-dirs: test + dependencies: + - leap + - hspec diff --git a/haskell/leap/src/LeapYear.hs b/haskell/leap/src/LeapYear.hs new file mode 100644 index 0000000000000000000000000000000000000000..0915a1a1f9e576d2d1fd3a4d5fbaba93bff2f54b --- /dev/null +++ b/haskell/leap/src/LeapYear.hs @@ -0,0 +1,4 @@ +module LeapYear (isLeapYear) where + +isLeapYear :: Integer -> Bool +isLeapYear year = rem year 4 == 0 && (rem year 100 /= 0 || rem year 400 == 0) diff --git a/haskell/leap/stack.yaml b/haskell/leap/stack.yaml new file mode 100644 index 0000000000000000000000000000000000000000..616941a41618158678b8290988bd44519442b2a5 --- /dev/null +++ b/haskell/leap/stack.yaml @@ -0,0 +1 @@ +resolver: lts-14.7 diff --git a/haskell/leap/test/Tests.hs b/haskell/leap/test/Tests.hs new file mode 100644 index 0000000000000000000000000000000000000000..fb0a245e2d90986dcbdb380d4f2d74d95640d9e3 --- /dev/null +++ b/haskell/leap/test/Tests.hs @@ -0,0 +1,64 @@ +{-# OPTIONS_GHC -fno-warn-type-defaults #-} +{-# LANGUAGE RecordWildCards #-} + +import Data.Foldable (for_) +import Test.Hspec (Spec, describe, it, shouldBe) +import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith) + +import LeapYear (isLeapYear) + +main :: IO () +main = hspecWith defaultConfig {configFastFail = True} specs + +specs :: Spec +specs = describe "isLeapYear" $ for_ cases test + where + + test Case{..} = it explanation assertion + where + explanation = unwords [show input, "-", description] + assertion = isLeapYear (fromIntegral input) `shouldBe` expected + +data Case = Case { description :: String + , input :: Integer + , expected :: Bool + } + +cases :: [Case] +cases = [ Case { description = "year not divisible by 4 in common year" + , input = 2015 + , expected = False + } + , Case { description = "year divisible by 2, not divisible by 4 in common year" + , input = 1970 + , expected = False + } + , Case { description = "year divisible by 4, not divisible by 100 in leap year" + , input = 1996 + , expected = True + } + , Case { description = "year divisible by 4 and 5 is still a leap year" + , input = 1960 + , expected = True + } + , Case { description = "year divisible by 100, not divisible by 400 in common year" + , input = 2100 + , expected = False + } + , Case { description = "year divisible by 100 but not by 3 is still not a leap year" + , input = 1900 + , expected = False + } + , Case { description = "year divisible by 400 in leap year" + , input = 2000 + , expected = True + } + , Case { description = "year divisible by 400 but not by 125 is still a leap year" + , input = 2400 + , expected = True + } + , Case { description = "year divisible by 200, not divisible by 400 in common year" + , input = 1800 + , expected = False + } + ]