andymac-2 / haskell-docs

MIT License
0 stars 3 forks source link

Added Quicksort and Yes programs #3

Open kahlil29 opened 6 years ago

andymac-2 commented 6 years ago

Hello again!

I was thinking along the lines of writing emulating the full Unix utility rather than just a function that implements the behavior of the utility. For example on the command line, yes has the following behaviour (we use head to stop it from running infinitely):

$ yes | head -n 10
y
y
y
y
y
y
y
y
y
y
$ yes "what's up?" | head -n 10
what's up?
what's up?
what's up?
what's up?
what's up?
what's up?
what's up?
what's up?
what's up?
what's up?

In Haskell this would probably look something like:

import qualified System.Environment as Env

main :: IO ()
main = do
    args <- Env.getArgs
    putStr . something $ args

something = error "TODO: Implement this function"

...and something similar for sort. These tasks are less about implementing the algorithm, and more about getting command line arguments, environment variables and using these to make a "complete" program. If you want to, you could use Data.List.sort to do the sorting for you.

Your version of sort doesn't need to be an exact copy of the sort utility, but maybe choose a few command line arguments which are easy enough to implement (Maybe a few like -r, -n, -t and -o, see man sort for details) You will most likely need System.Console.GetOpt for this.

For reference, sort has the following behaviour:

$ sort -n
6
3
9
0
1
5
2
8
7
4
(Ctrl + D)
0
1
2
3
4
5
6
7
8
9
kahlil29 commented 6 years ago

Okay I didn't know that this is what you were expecting. Thanks for explaining it in detail! I don't know the unix command-line options in so much depth, but your examples make it quite simple to understand! 😄

I'll go through what you've given and try to emulate the same behavior in my code.