Skip to content

Instantly share code, notes, and snippets.

@harpresing
Last active December 7, 2016 16:34
Show Gist options
  • Save harpresing/edf0cbdeae83984b2f12a7c63be0f512 to your computer and use it in GitHub Desktop.
Save harpresing/edf0cbdeae83984b2f12a7c63be0f512 to your computer and use it in GitHub Desktop.

Introduction

In purely functional programming you don't tell the computer what to do as such but rather you tell it what stuff is. The factorial of a number is the product of all the numbers from 1 to that number, the sum of a list of numbers is the first number plus the sum of all the other numbers, and so on. You express that in the form of functions.

The only thing a function can do is calculate something and return it as a result.

Haskell is lazy. That means that unless specifically told otherwise, Haskell won't execute functions and calculate things until it's really forced to show you a result.

Haskell is statically typed. When you compile your program, the compiler knows which piece of code is a number, which is a string and so on. That means that a lot of possible errors are caught at compile time. If you try to add together a number and a string, the compiler will whine at you. Haskell uses a very good type system that has type inference.

Type inference also allows your code to be more general. If a function you make takes two parameters and adds them together and you don't explicitly state their type, the function will work on any two parameters that act like numbers

Haskell GHCI - The interactive mode is invoked by typing in ghci at your prompt. If you have defined some functions in a file called, say, myfunctions.hs, you load up those functions by typing in :l myfunctions and then you can play with them, provided myfunctions.hs is in the same folder from which ghci was invoked. If you change the .hs script, just run :l myfunctions again or do :r, which is equivalent because it reloads the current script.

Haskell Functions

So + , * , / are examples of what we call an infix function. Most functions that aren't used with numbers are prefix functions. Let's take a look at them.

In Haskell, functions are called by writing the function name, a space and then the parameters, separated by spaces. Function application (calling a function by putting a space after it and then typing out the parameters) has the highest precedence of them all. What that means for us is that these two statements are equivalent.

Prelude> succ 9 + max 10 11 + 2
23

If a function takes two parameters, we can also call it as an infix function by surrounding it with backticks. For instance, the div function takes two integers and does integral division between them.

Baby's First Functions

doubleme x = x + x
doubleus x y = doubleme x + doubleme y

Haskell If statement

The difference between Haskell's if statement and if statements in imperative languages is that the else part is mandatory in Haskell. If statement in Haskell is an expression. An expression is basically a piece of code that returns a value. 5 is an expression because it returns 5, 4 + 8 is an expression, x + y is an expression because it returns the sum of x and y. Because the else is mandatory, an if statement will always return something and that's why it's an expression.

doubleSmallNumber x = if x > 100  
                        then x  
                        else x*2   
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment