haskell.pdf - INTRODUCTION TO HASKELL PRINCIPLES OF...

This preview shows 1 out of 17 pages.

INTRODUCTION TO HASKELL PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2018 Dalhousie University 1/81
Image of page 1

Subscribe to view the full document.

HASKELL: A PURELY FUNCTIONAL PROGRAMMING LANGUAGE • Functions are first-class values: Can be passed as function arguments, returned from functions. 2/81
Image of page 2
HASKELL: A PURELY FUNCTIONAL PROGRAMMING LANGUAGE • Functions are first-class values: Can be passed as function arguments, returned from functions. Variables are (normally) immutable. 2/81
Image of page 3

Subscribe to view the full document.

HASKELL: A PURELY FUNCTIONAL PROGRAMMING LANGUAGE • Functions are first-class values: Can be passed as function arguments, returned from functions. Variables are (normally) immutable. • Deeply grounded in the mathematics of computing. 2/81
Image of page 4
HASKELL: A PURELY FUNCTIONAL PROGRAMMING LANGUAGE • Functions are first-class values: Can be passed as function arguments, returned from functions. Variables are (normally) immutable. • Deeply grounded in the mathematics of computing. • Effectful computations are modelled in a functional manner. 2/81
Image of page 5

Subscribe to view the full document.

HASKELL: A PURELY FUNCTIONAL PROGRAMMING LANGUAGE • Functions are first-class values: Can be passed as function arguments, returned from functions. Variables are (normally) immutable. • Deeply grounded in the mathematics of computing. • Effectful computations are modelled in a functional manner. • Elegant and concise. 2/81
Image of page 6
VALUES AND FUNCTIONS In Haskell, functions are values and values are (nullary) functions. 3/81
Image of page 7

Subscribe to view the full document.

VALUES AND FUNCTIONS In Haskell, functions are values and values are (nullary) functions. C++: int x = 2; Haskell: x :: Int x = 2 3/81
Image of page 8
VALUES AND FUNCTIONS In Haskell, functions are values and values are (nullary) functions. C++: int x = 2; Haskell: x :: Int x = 2 int x() { return 2; } x :: Int x = 2 3/81
Image of page 9

Subscribe to view the full document.

VALUES AND FUNCTIONS In Haskell, functions are values and values are (nullary) functions. C++: int x = 2; Haskell: x :: Int x = 2 int x() { return 2; } x :: Int x = 2 int add(int x, int y) { return x + y; } add :: Int -> Int -> Int add x y = x + y 3/81
Image of page 10
LOCAL VARIABLES Local variables are useful in many programming languages to store intermediate results. Haskell is no different. The following two pieces of code behave identically: veclen :: (Float, Float) -> Float veclen (x, y) = sqrt(xx + yy) where xx = x * x yy = y * y veclen :: (Float, Float) -> Float veclen (x, y) = let xx = x * x yy = y * y in sqrt(xx + yy) 4/81
Image of page 11

Subscribe to view the full document.

VARIABLES ARE IMMUTABLE C++: Haskell: int four() { int x = 2; x = x + 2; return x; } four :: Int four = x where x = 2 x = x + 2 … returns 4. … gives a compile-time error. 5/81
Image of page 12
VARIABLES ARE IMMUTABLE C++: Haskell: int four() { int x = 2; x = x + 2; return x; } four :: Int four = x2 where x1 = 2 x2 = x1 + 2 … returns 4. … works. 5/81
Image of page 13

Subscribe to view the full document.

VARIABLES ARE IMMUTABLE C++: Haskell: int four() { int x = 2; x = x + 2; return x; } four :: Int four = x2 where x2 = x1 + 2 x1 = 2 … returns 4. … also works. 5/81
Image of page 14
CONTROL CONSTRUCTS if-then-else: abs :: Int -> Int abs x = if x < 0 then (-x) else x 6/81
Image of page 15

Subscribe to view the full document.

CONTROL CONSTRUCTS if-then-else: abs :: Int -> Int abs x = if x < 0 then (-x) else x The else-branch is mandatory! Why?
Image of page 16
Image of page 17
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern