### Josh Finnie PRO

Software Engineer based out of Washington DC. Working at PBS!

Day-O-Haskell

Josh Finnie

Software Maven @ TrackMaven

Functional Programming is the practices of writing code using solely functions avoiding both changing state and mutable data. [1]

[1] https://en.wikipedia.org/wiki/Functional_programming

- Side-effect Free - programmed in such a way where each function is isolated from each other.
- Easy to Optimize - Since each function is isolated, it can be its best form without worrying how it effects other functions
- Easy to Test -
*See Above* - Easy to Parallelize -
*See Above*

I don't know... and I am sad about choosing it :-)

A standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. [1]

[1] https://en.wikipedia.org/wiki/Haskell_(programming_language)

```
Prelude> putStrLn "Hello, world!"
Hello, world!
Prelude> :t putStrLn
putStrLn :: String -> IO ()
```

Above is the "Hello World!" example in Haskell along with showing the definition of the `putStrLn` function.

```
Prelude> let fac n = if n == 0 then 1 else n * fac (n-1)
Prelude> fac 10
3628800
```

Above is the "fibonacci sequence" example in Haskell. It's pretty cool how it's recursive and easy!

```
import System.Random
import Control.Monad(when)
isValidNumber n = do
n > 0 && n < 10
testGuessedNumber a b = do
if a == b
then putStrLn "You're correct!"
else putStrLn $ "Sorry, the correct answer was " ++ show a
main = do
gen <- getStdGen
let (randNumber, _) = randomR (1,10) gen :: (Int, StdGen)
putStr "Which number in the range from 1 to 10 am I thinking of? "
numberString <- getLine
when (not $ null numberString) $ do
let number = read numberString
if isValidNumber number
then testGuessedNumber randNumber number
else putStrLn $ "Please select a number between 1 and 10!"
newStdGen
main
```

```
import System.Random -- LOL
randomNumber = 4 -- chosen by fair dice roll.
-- guaranteed to be random.
isValidNumber :: Int -> Bool
isValidNumber n
| n > 0 && n < 10 = True
| otherwise = False
testGuessedNumber :: Int -> Int -> Bool
testGuessedNumber a b
| a == b = True
| otherwise = False
getInt :: IO Int
getInt = do
num <- getLine
return $ (read num :: Int)
main :: IO ()
main = do
putStr "Which number in the range from 1 to 10 am I thinking of? "
number <- getInt
if isValidNumber number
then run randomNumber number
else putStrLn "please select a number between 1 and 10."
run :: Int -> Int -> IO()
run r n
| outcome == True = do
putStrLn "You Win!"
| outcome == False = do
putStrLn "You guessed incorrectly, please try again."
number <- getInt
run r number
where outcome = testGuessedNumber r n
```

I was impressed with a lot of what Haskell had to offer. Would I use it again? - No...

Code

https://github.com/joshfinnie/haskell-guessing-game

By Josh Finnie

- 2,097

Software Engineer based out of Washington DC. Working at PBS!

Loading comments...