A Pythonista's

Josh Finnie

Software Maven @ TrackMaven

Functional Programming

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

What does that mean?

  • 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

Why Haskell?

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

What is Haskell?

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)

What is Haskell? (Con't)

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.

What is Haskell? (Con't)

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

Prelude> fac 10

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

Let's get serious (Guessing Game)

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!"

Let's get more serious!

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...





A Pythonista's Day-O-Haskell

By Josh Finnie

A Pythonista's Day-O-Haskell

  • 404
Loading comments...

More from Josh Finnie