Skip to content

Instantly share code, notes, and snippets.

@mjgpy3
Last active November 21, 2023 19:54
Show Gist options
  • Save mjgpy3/215f3131412841946e35d02a953e2c87 to your computer and use it in GitHub Desktop.
Save mjgpy3/215f3131412841946e35d02a953e2c87 to your computer and use it in GitHub Desktop.
Learn Haskell by Building (Assignment)

Learn Haskell by Building

Congratulations and welcome to the new VC-funded startup Kewl-Word-Divination.ai! We may not have a working product yet but that's where you come in! (Don't worry we've got a few months of runway).

Since we're following a new methodology -- the Scant Agile Framework -- we'll be building our product out iteratively (the way the Mona Lisa was painted).

v0.01 - kwd CLI

We're gonna need a CLI program that helps us search for keywords in a body of text. We'll evolve this over time to become our text divination platform!

In the command line program, hard-code a list of key words for which we will search

keywords = ["blue", "green", "yellow", "orange", "red", "purple"]

Basic usage of the program should look like the following

$ stack run -- "My favorite color is blue!"
At least one keyword found!

$ stack run -- "My favorite color is teal!"
No keywords found :(

Requirements

  • Use stack (see stack's getting started page for help)
  • Exact text matching is good enough for v0.01 ("red" can match "scared")
  • Hard-code the keywords in the program

Optional things you may wanna explore

  • Try something other than stack (e.g. nix) to bootstrap and run the project
  • If you used String try Text instead (or vice versa)
  • Search around stackage for packages
  • Search around hoogle for functions
  • Use a package to colorize the program's output in a fun way
  • Error handling if a users passes the wrong number of arguments

v0.02 - Text, Text everywhere but not a Char to String

Hey, thanks for building that tool, it's legit! We'll be sure to send a few kwd reward points (kewards) your way...

I think we forgot to mention that kwd should ignore case when matching words! So, for example, "I'm Blue abade" should be matched by the "blue" keyword...

Oh, and you know how we said that the "red" keyword can match "scared"? We've been running this on a lot of spooky texts lately and getting a lot of false positives! Can you make it so that the tool respects word boundaries?

We've also reached out to famous Haskell consultant Edwin K. Hackepeter and he said something about how we should be using Data.Text and avoiding String. Could you look into that also?

Requirements

  • Case-insensitive. E.g. keyword "scary" should match "SpOoKy sCaRy sKeLeToNs", keyword "SCARY" should behave likewise
  • Respect word bounds. E.g. keyword "pumpkin" should not match "Jack the pumpkinking." but it should match "Carve a pumpkin."
  • Use Data.Text.

Optional things you may wanna explore

  • Why prefer Text? When is String good?
  • You could try regex for fun if you like
  • Look into GHC extensions, specifically OverloadedStrings

v0.03 - Rank Requirements

This tool's really taking off, good work. The only downside so far is that, if it finds a match, we have to manually search to see what keyword's been found. Let's fix that!

To start, could you output the count of each keyword that's found? For example

$ stack run -- "purple Orange orange blue aqua blue green red teal"
Found keywords:
purple: 1
orange: 2
blue: 2
green: 1
red: 1

Also, it would be nice to know how many words are not keywords. For example

$ stack run -- "orange orange blue aqua blue green red teal"
Found keywords:
# (omitted for concision)

Non-keyword count: 2

Also, we've been searching some fairly large texts! If you have time, would you mind sorting the keyword counts, descending, in the output?

$ stack run -- "orange orange blue aqua blue green red teal"
Found keywords:
blue: 2
orange: 2
green: 1
red: 1

Non-keyword count: 2

Requirements

  • Output each keyword that's found, as well as a count of occurrences.
  • Output how many non-keywords were present in the text.
  • Sort keyword counts by count, descending.
  • If a keyword isn't matched, don't include it in the output.

Optional things you may wanna explore

  • Are there libraries that "just" do this?
  • Are there different ways/libraries for formatting this output?

v0.04 - Testing 1 2 3

Great work! But did you break anything? Are ya sure?

Please write some automated tests that cover what we've built so far

  • actual matching logic
  • sorting logic
  • counting logic

Bonus points if you cover edge cases that aren't mentioned in these assignments.

Requirements

  • Write tests to adequately cover the assignments as specified so far
  • If you're unfamiliar/uncomfortable with hspec then use it. If you're familiar and comfortable with hspec use something else.
  • Write at least 1 property-based test

Optional things you may wanna explore

  • Try a new testing library
  • Compare a few libraries
  • Try to test the executable itself, rather than just writing unit tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment