Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Functional Programming - A Practical Exploration

Ritom Gupta
September 19, 2018

Functional Programming - A Practical Exploration

A presentation lecture by Ritom Gupta as a part of the "Principles of Programming Language and Functional Programming" course, under School of Computer Engineering and Technology, MIT World Peace University.
This presentation encompasses the necessity of functional languages, how they compare to traditional and widely used imperative languages, actual codes in Python, Haskell and Lisp for certain chosen questions, along with some information about their use by major technology-based companies.

To get a copy of the slide-by-slide notes made for the lecture, please contact the author at: [email protected]

Ritom Gupta

September 19, 2018
Tweet

More Decks by Ritom Gupta

Other Decks in Programming

Transcript

  1. groceries = [”apple”, ”banana”, ”orange”, ”strawberries”, ”cherries”] basket = []

    def get_groceries(): for item in groceries: if item not in basket: basket.append(item) print(basket)
  2. x = 1 def times_two(): global x x = x*2

    times_two() print(x) #=> 2 times_two() print(x) #=> 4
  3. oddeven n = if n `rem` 2 == 0 then

    putStrLn "Number is Even" else putStrLn "Number is Odd" main=do putStrLn "Enter a number:" x <- readLn oddeven x
  4. def bubbleSortasc(arr): n = len(arr) for i in range(n): for

    j in range(0, n-i-1): if arr[j] > arr[j+1] : arr[j], arr[j+1] = arr[j+1], arr[j] arr = [] elem = int(input("Number of elements you want:")) for i in range(0, elem): arr.append(int(input("Enter number :"))) bubbleSortasc(arr) print ("Array sorted in ascending order is:") for i in range(len(arr)): print ("%d" %arr[i]),
  5. #Computes result from marks of 3 subjects def result(m): if

    m > 80: print("\nYou passed with distinction!") elif m <= 80 & m > 60: print("\nYou passed with First Class!") elif m <= 60 & m > 50: print("\nYou passed with Second Class.") elif m <= 50 & m > 40: print("\nYou passed.") else: print("\nYou failed.") def average(temp_one,temp_two,temp_three): return (temp_one + temp_two + temp_three) / 3
  6. print("Enter marks for 3 subjects:") a=int(input("1st subject:")) b=int(input("2nd subject:")) c=int(input("3rd

    subject:")) avg=average(a,b,c) print("\nYour average mark is:" '{:.1f}'.format(avg)) result(avg)
  7. def area_ground(l,b): return l * b def perim_ground(l,b): return 2*(l

    + b) print("Enter dimensions of the ground:") x=int(input("Enter length:")) y=int(input("Enter breadth:")) print "\nArea of the ground=", area_ground(x,y), " unit^2" #this works in Python2 only. Syntax error in Python3 print "Perimeter of the ground=", perim_ground(x,y), "units"
  8. (defun area_ground(l b) (format t "Area of the ground=: ~5f

    unit^2" (* l b)) ) (defun perim_ground(l b) (terpri) (format t "Perimeter of the ground=: ~5f units" (* 2 (+ l b))) ) (princ "Enter length and breadth:") (terpri) (defvar x) (defvar y) (setq x (read)) (setq y (read)) (terpri) (area_ground x y) (perim_ground x y)