Skip to content

nullnullnullnullnullnullnullnullnullnul/haskell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

haskell

A collection of Haskell exercises focused on functional programming fundamentals, covering topics such as guards, pattern matching, monads, lambda expressions, and stateful computation.

Prerequisites

  • GHC (Glasgow Haskell Compiler)

Structure

1/
├── e1.hs    Strict maximum of three integers
├── e2.hs    Date validation with leap year handling
├── e3.hs    Subway fare calculator with tiered discounts
├── e4.hs    Change calculator using the State monad
├── e5.hs    Oblong and triangular number checks via lambdas
├── e6.hs    Integer concatenation through type casting
└── e7.hs    Next-day date computation

Exercises

e1 - Strict Maximum

Returns the largest of three positive integers only if it is unique (strict maximum). Returns -1 if no strict maximum exists. Implemented without logical operators.

e2 - Date Validation

Validates a date given as day, month, and year. Handles month-specific day limits and leap years using guards.

e3 - Subway Fare Calculator

Computes the total monthly subway cost using a tiered discount system based on the number of trips:

Trips Discount
1 - 20 None
21 - 30 20%
31 - 40 30%
40+ 40%

Uses Maybe to signal when no discount applies.

e4 - Change Calculator

Determines the minimum number of bills to return as change for a purchase. Uses Control.Monad.State and Data.IntMap.Strict to track bill denominations ($5000, $1000, $500, $200, $100, $50, $10).

e5 - Oblong and Triangular Numbers

Lambda-based checks for:

  • Oblong numbers: products of two consecutive naturals (e.g. 6 = 2 * 3).
  • Triangular numbers: sums of consecutive naturals starting from 1 (e.g. 10 = 1+2+3+4).

e6 - Integer Concatenation

Concatenates two positive integers by converting them to strings, joining, and reading back (e.g. 1234 and 567 becomes 1234567).

e7 - Next Day

Computes the day following a given date. Handles month boundaries, year transitions, and leap years. Includes custom date parsing and format validation.

Running

Compile and run any exercise with GHC:

ghc -o e1 1/e1.hs && ./e1

Or interpret directly with runhaskell:

runhaskell 1/e1.hs

Concepts Covered

  • Guards and pattern matching
  • Maybe for optional results
  • State monad for stateful computation
  • IntMap for efficient key-value storage
  • Lambda expressions
  • IO monad for user interaction
  • Type casting with read / show
  • Text.Printf for formatted output

About

Haskell exercises covering guards, pattern matching, monads, lambdas, and stateful computation.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors