SlideShare uma empresa Scribd logo
1 de 17
Baixar para ler offline
Functional
Programming
Submitted by:
Chad McQuinn,Nicholas Armstrong,Shreyas C.S & Shreya
Chakrabarti
Abstract
In the project, we have studied, applications that can use functional programming languages.
We have studied the numerous approaches for using Functional Programming languages in
applications like Algorithms,Data-Mining,Game Programming and Data Visualization.
In this report we are going to discuss those applications on the basis of performance,
understandability, readability etc. This report contains the information about these applications
in various functional languages and the analysis that we have performed on those languages.
Rajeev Raje | CS 56500 - Programming Languages | Project Report | 12/12/2016
1
Table of Contents
Table of Cont​ents
Introduction to Functional Programming Languages and Problem Statement 2
Proposed Approach and Outcomes 2
Applications of Functional Programming Languages 2
Algorithms in Functional Programming 3
Data Mining in Functional Programming 5
Game Programming in Haskell 6
Data Visualization 7
Observations 8
Conclusions 9
Critique 9
Appendix 10
Functional Programming | References 1​4
Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
2
Introduction to Functional Programming Languages and Problem Statement
The main motivation behind developing a Functional Programming paradigm was to make the
behaviour of a program predictable.Functional programming was thus developed based on
Mathematics namely to maintain its predictable nature. Functional Programming language finds
its roots in “Lambda Calculus” and as indicated in the name itself Functional Programming
languages operates using functions.Due to its inherent property of avoiding changing state and
mutable data function,functions in these type of programming languages provide same output
every-time the same function is called.
Although there are many more programming languages based on functional programming,
some of the most popular pure functional languages which find applications in the real world
include Haskell, Hope, Erlang, Clojure, and Common Lisp.
Our problem is to use some of these languages to study their implementation in some of the
popular applications in computer science.These applications were chosen as the demand for
computations in some of these areas is higher than others and application of languages which
use a functional paradigm and are mathematical in nature would be of greater use in this area.
Proposed Approach and Outcomes
We will examine functional approaches to solving various problems in computer science and
software engineering. Our selected problems are Algorithms (in particular, sorting), Data Mining,
Game Programming, and Data Visualization.
In each section, we will describe how well functional programming fits the problem, using
measures such as code simplicity, performance (time/space complexity), popularity, and other
factors.
Applications of Functional Programming Languages
The various areas of Computer Science which we thought would benefit from the inherent
properties of Functional Programming languages are discussed in the following section.
Fig 1 is a pictorial representation of the properties of Functional Programming Languages that
we found might be helpful in the applications discussed after the figure.
Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
3
​ Fig:1
Algorithms in Functional Programming
Algorithms boast of being the heart of computer science field. Problem solving using Algorithms
has found widespread application in areas beyond Computer Science as well.We tried to
implement a merge sort algorithm in a Functional and Non-Functional Programming Language.
Consider the example sorting- Mergesort algorithm in Python and Haskell:
Python Haskell
1. List is split into two parts
def merge(a, b):
if len(a) == 0:
return b
elif len(b) == 0:
return a
elif a[0] < b[0]:
return [a[0]] + merge(a[1:], b)
else:
1. List is split into two parts
mergesort'splitinhalf :: [a] -> ([a], [a])
mergesort'splitinhalf xs = (take n xs, drop
n xs)
where n = (length xs) `div` 2
Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
4
return [b[0]] + merge(a, b[1:])
2. Two parts are sorted by the algorithm
3. The sorted parts are merged by a special
merging procedure for sorted lists
def mergesort(x):
if len(x) < 2:
return x
else:
h = len(x) // 2
return merge(mergesort(x[:h]),
mergesort(x[h:]))
2. Two parts are sorted by the algorithm
mergesort'merge :: (Ord a) => [a] -> [a] ->
[a]
mergesort'merge [] xs = xs
mergesort'merge xs [] = xs
mergesort'merge (x:xs) (y:ys)
| (x < y) = x:mergesort'merge xs (y:ys)
| otherwise = y:mergesort'merge (x:xs)
ys
3. The sorted parts are merged by a special
merging procedure for sorted lists
mergesort :: (Ord a) => [a] -> [a]
mergesort xs
| (length xs) > 1 = mergesort'merge
(mergesort ls) (mergesort rs)
| otherwise = xs
where (ls, rs) = mergesort'splitinhalf xs
About Python syntax:
● L[0] is the first element of list L
● L[1:] is the list of all remaining
elements
● More generally L[:n] is the list of up to
the n-th element, L[n:] the rest
● A + B if A and B are both lists is the
list obtained by concatenation
● [x] is a list containing just the single
element x
About Haskell syntax:
mergesort’splitinhalf : returns a pair of
arrays into which the original array was split.
n is equal to the half of the length of the
array, and then we use the standard
functions ​take​ and ​drop​.
take n xs ​: Get the first n elements of the list
drop n xs : rest of the list after those first n
elements
Function for merging two sorted arrays:
The function receives two arrays and
produces one array of the same type.
The algorithm for merging:
1. If the first list is empty [] then the result of
the merge is the second list xs
2. If the second list is empty [] then the result
of the merge is the first list xs
Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
5
3. Otherwise we compare the first elements
of the lists and append with the colon :
function the least of them to the new list
which is the result of merging the remaining
two lists
Function mergesort:
If the length of the list is greater than 1 then
we do the standard step of the algorithm.
Otherwise the list with the length of 1 is
already sorted (the condition for ending the
recursion). The code reads exactly the same
as the textual description of the algorithm
given earlier but now this is a formal and
shorter specification.
Data Mining in Functional Programming
With the increasing advent of Data being used in nearly every possible field in the real world.
Data driven decision making and improving performances of tools already in place to operate on
this data is becoming vital with every passing day.
Functional Programming language was thought to find a great application in the field because of
its strengths of Parallelism,Transformations and Ability to perform faster computations.
We used an example in Non-Functional,Functional and a hybrid language to make observations
of data mining tasks done in these respective languages. Code for these examples is mentioned
in the appendix.
Parameters Data Mining in
Non-Functional
Language
(Python)
Data Mining in
Functional Language
(Haskell)
Data Mining in Hybrid
Language
(Scala)
Level of Difficulty to
learn
Easy Difficult Moderate
Orientation Analytical Oriented
Language
Mathematically oriented
Language
Engineering Oriented
Language
Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
6
Data Parsing One data type can be
easily parsed as other
data type
The fields are statically
typed and it cannot be
passed as any other
data type,if tried it
would raise a compile
error
Data parsing although not
inherent to scala it uses XML
packages for data parsing
Memory Usage The entire data file is
loaded into memory
Data is achieved in
streaming fashion, with
constant memory usage
Data file is loaded into
memory
Calculations Functions need to be
written to perform
further advanced
calculations on the data
Inherent Mathematical
features make it very
easy to perform
advanced calculations
Inherent Functional
Programming features allow
faster computations in Scala
There was no significant difference found in execution times for examples run in the above
languages.The poll in the appendix of this paper, and other resources on the internet show that
using functional programming languages despite their mathematical abilities are not a popular
choice amongst practitioners of Data Mining in the real world.
Other Data Mining Tools based on Functional Programming Languages
● Common Lisp
● Clojure (Dialect of Common Lisp)
● Julia- Although not functional because of its ability to support extensive mathematical
function library it is considered functional many a times
● PolyFARM - Data Mining application developed entirely in Standard Haskell
Game Programming in Haskell
Not many commercial games have been written in Haskell (see references). Henrik Nilsson &
Ivan Perez presented at PDP14 (Principles and Practice of Declarative Programming) on a
breakout game written in Haskell as a proof of concept. Their premise was to use features of
declarative functional programming to view games as a “network of signals and signal
transformers”. Their proof of concept included multiple levels and even background music.
Ultimately this was expanded into a full Android game.
These authors were involved with Keera Studios and have published a blog describing
optimizing a game that was producing 10-15 FPS (frames per second) on Android and 60-70
FPS (frames per second) on desktop machines. Ultimately through optimization techniques,
they achieved 500 FPS on desktop machines.
Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
7
After realizing that traditional functional programming was in some ways not a good fit for game
programming, they turned to Functional Reactive Programming (FRP), which allows for a
functional language to describe entities that change over time. These changes can be discrete
or continuous, and the authors found a Haskell package, Yampa, that supports both models (4).
Their first approach was to profile the application. They were examining memory usage, speed
(primarily measured in FPS), and impact of the changes (simplicity of the required code).
Time and Space Complexity
They first determined where time was being spent and how much memory was being
consumed. Initially some optimizations were applied to native libraries being used by the game,
but this resulted in a relatively small increase of 20-30 FPS.
They next found that Haskell implementation was consuming hundreds of megabytes of
memory. Worse, memory usage was growing dramatically over time. This was largely due to
lazy evaluation. Since individual object fields are not evaluated until they are needed, an entire
object must live until this evaluation is performed--even if it is ultimately never needed. This
resulted in a large amount of memory growth over time. By using ​strictness(6) (which
overcomes the lazy evaluation) they were able to overcome this. This reduced memory
consumption and increased FPS dramatically. The desktop version increased from 90 FPS to
over 300 FPS with this change. Memory usage also exhibited a flat profile with a steady usage
of around 3 megabytes. Here is an example of strictness in Haskell: the language’s ​Complex
data type
data RealFloat a => Complex a = !a :+ !a
This definition forces both components to be evaluated at definition time; a Complex instance
will not live on simply because (for example) its imaginary component has not been evaluated
yet.
Simplicity of Code
The authors describe code changes to achieve parallelization that in many cases were
extremely simple--as few as 5 or 10 lines of code increasing parallelization and consequently
FPS by 10% or more. Some easy parallelization increased the desktop version to over 500
FPS. However, Android performance had only been increased to around 25 FPS. At this
framerate, collisions were not precise and the physics of the game were not realistic, with
objects passing through each other, etc. By isolating the physics logic from the renderer and
parallelizing this way, they were able to achieve physics evaluation at over 30 FPS while the
renderer stayed at 15 FPS. This resulted in much more satisfactory physics behavior. This
parallelization for Android required just a few new objects and 10 lines of code.
Data Visualization
One of the key issues with visualising data in functional program involves its inherent tree like
structure. As mentioned in the “A Survey of Program Visualizations for the Functional
Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
8
Paradigm” there are situations involving lazy evaluation where these trees become directed
graphs. Also noted there are several utilities that handle this visualization including CIDER,
KIEL, Prospero, Hint, and TERSE. Dealing with large trees or directed graphs has its own
concerns so several of these utilities provide ways of compressing these graphs. Some utilities
provide filters for visualizing data in the spatial sense and temporal sense. Some of these
utilities reduce the tree(graph) by minimizing regions that are trivial evaluations. One thing that
is noticeable about these utilities is that it attaches itself to a paradigm that is quite commonly
learned in intro computer science classes. The flow chart.
Another aspect to consider is UML. As noted in “Metamodel and UML Profile for Functional
Programming Languages” UML was originally intended to be a universal general purpose
modeling language. Applying UML to functional programming requires some modification. One
of those modifications ties into the functional language Haskell referred to as the Haskell
metamodel. The metamodel consists of three class diagrams. Haskell programs are organized
into modules. Two program elements are represented in a module, functions and user data
types.
Observations
1. ​Higher-order functions​ (which let a person pass functions to functions, or return functions
from functions) let a person factor out a lot of duplication in the small. In algorithms section we
refactored the existing Java app to use higher order functions, and found and fixed several bugs
along the way (all related to bugs by copy & paste).
2. ​Immutable data structures​, which are often used in FP, free an individual from having to
constantly worry about what the code will do to the data which is passed into it. In the Java app,
we found a lot of “defensive copying”, which could simply be deleted as we changed many core
data structures from mutable to immutable.
3. ​Strong types​, which appear in many functional programming languages (but not all), tell us
more about statically-proven properties of the code. In the Java app, we changed a lot of code
from using null to using a generic optional data structure, which more clearly communicates
where values may be absent. This let us delete a lot of defensive null-checking, as well as fix a
few NPEs in uncommon code paths.
4. ​Pure functions​, which are functions that don’t have side effects (i.e. their output is a
deterministic function of their input), are much easier to understand and test, because one
doesn’t have to wonder if the function’s behavior will change based on hidden state. In the Java
application, we were able to convert a lot of stateful functions to stateless functions, which made
the code much simpler and fixed a few bugs.
Some of the reasons we found out,why functional programming languages are not a popular
choice amongst people from all of these areas are listed as below:
● Functional languages forbid state and traditional iteration structures
Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
9
● Absent ​side effects, all computation is done by evaluating functions, and these functions
should behave the same way any time they are called with precisely the same
arguments
● Functional programs forbid assignment
● All loops must be implemented with recursion. This might seem absurdly restrictive
● Difficult to learn
Conclusions
Although we tried to find topics that played to the strengths of functional programming, we did
not find many real-world uses of pure functional programming. In Data mining, we found that
hybrid languages(Scala) are much more popular. Some of the most common computer science
algorithms do not lend themselves to a pure functional model. Although the authors we
surveyed showed that you can write a game using pure functional programming, this still isn’t a
popular choice. The relative lack of popularity of pure functional programming has also led to
there being few tools for visualization.
Critique
Goals of Software Engineering:
1. Code that works reliably, even in parts of the application that aren’t used very often.
2. Code that can be easily understood by other people (The person writing the code won’t be
around forever).
3. Code that is factored in such a way as to minimize the cost of changing it (because if one
thing is certain, it’s that requirements never stop changing).
(They also usually want it fast and cheap, too, but that’s a topic for another post.)
For our algorithms section, despite the fact that this program is “purely functional”, the
code is complete and utter garbage:
1. It had several bugs when first written, and it took a lot of time to track them down.
2. It’s hard to understand. In fact, a C implementation would probably be easier to understand.
3. For such a small function, it would be awfully hard to maintain. Making changes safely would
require a large amount of mental effort and testing, and you might not be able to reuse much
code.
More generally, our original goal was to survey more topics.Lack of information in many areas,
lead us on including only the four in the paper. We also wanted to find use cases for which
functional programming was a good fit, a poor fit, or somewhere in-between, but this judgment
was very subjective and we dropped it.
Functional languages are increasingly being adopted by industry in the real world.
To name a few examples, Haskell used by Facebook makes its news feeds run smoothly;
WhatsApp relies on Erlang to run messaging servers, achieving up to 2 million connected users
per server; Twitter, LinkedIn, Foursquare, Tumblr, and Klout rely on the hybrid, Scala to build
Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
10
their core infrastructure for sites. Indirect application of functional programming features include
Google’s popular Map-Reduce algorithm which derives its map and reduce functions commonly
found in functional programming.
What would we do if we were starting over today? We would probably try to do more
comparison of functional programming against other paradigms, and do more presentation of
measurable results.
Appendix
Data Mining Poll
Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
11
(http://www.kdnuggets.com/2012/08/poll-analytics-data-mining-programming-languages.html)
Code for Data Mining
1) Code Snippet for Data Mining in Python
import zipfile
import requests
import numpy as np
import pandas as pd
Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
12
import seaborn as sns
import matplotlib.pyplot as plt
r = requests.get('https://github.com/datailluminations/PredictingFlightsDelay',
stream=True)
with open("flights.csv", 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
zf = zipfile.ZipFile("flights.csv.zip")
filename = zf.filelist[0].filename
fp = zf.extract(filename)
df = pd.read_csv(fp, parse_dates="FL_DATE").rename(columns=str.lower)
2) ​Code Snippet for Data Mining in Haskell
{-# LANGUAGE TypeApplications, OverloadedStrings, OverloadedLabels,
TypeOperators, DataKinds, FlexibleContexts #-}
import Labels.Explore
main =
runResourceT $
httpSource "https://github.com/datailluminations/PredictingFlightsDelay" responseBody
.|
zipEntryConduit "ontime.csv" .|
fromCsvConduit
@("fl_date" := Day, "tail_num" := String)
(set #downcase True csv) .|
dropConduit 10 .|
takeConduit 5 .>
tableSink
3) ​Code Snippet for Data Mining in Scala
import java.io.File
import scala.io.{ BufferedSource, Source }
import sys.process._
import java.net.URL
import java.io.File
new URL("https://github.com/datailluminations/PredictingFlightsDelay") #> new
File("Output.html")
abstract class StackTable[T] {
Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
13
val file: File
def getDate(n: scala.xml.NodeSeq): Long = n.text match {
case "" => 0
case s => dateFormat.parse(s).getTime
}
def dateFormat = {
import java.text.SimpleDateFormat
import java.util.TimeZone
val f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS")
f.setTimeZone(TimeZone.getTimeZone("GMT"))
f
}
def getInt(n: scala.xml.NodeSeq): Int = n.text match {
case "" => 0
case x => x.toInt
}
def parseXml(x: scala.xml.Elem): T
def parse(s: String): Option[T] =
if (s.startsWith(" <row ")) Some(parseXml(scala.xml.XML.loadString(s)))
else None
}
Code for Merge sort
1) Code Snippet for Merge sort in Haskell
mergesort'splitinhalf :: [a] -> ([a], [a])
mergesort'splitinhalf xs = (take n xs, drop n xs)
where n = (length xs) `div` 2
mergesort'merge :: (Ord a) => [a] -> [a] -> [a]
mergesort'merge [] xs = xs
mergesort'merge xs [] = xs
mergesort'merge (x:xs) (y:ys)
| (x < y) = x:mergesort'merge xs (y:ys)
| otherwise = y:mergesort'merge (x:xs) ys
mergesort :: (Ord a) => [a] -> [a]
mergesort xs
| (length xs) > 1 = mergesort'merge (mergesort ls) (mergesort rs)
| otherwise = xs
where (ls, rs) = mergesort'splitinhalf xs
Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
14
2) Code Snippet for Merge sort in Python
def merge(a, b):
if len(a) == 0:
return b
elif len(b) == 0:
return a
elif a[0] < b[0]:
return [a[0]] + merge(a[1:], b)
else:
return [b[0]] + merge(a, b[1:])
def mergesort(x):
if len(x) < 2:
return x
else:
h = len(x) // 2
return merge(mergesort(x[:h]), mergesort(x[h:]))
Functional Programming | References
Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
15
Image on Report Title:
http://programminghints.com/2016/04/need-fu
nctional-programming/
Introduction to Functional Programming:
https://en.wikipedia.org/wiki/Functional_progr
amming
http://comjnl.oxfordjournals.org/content/32/2/
98.short
http://wiki.c2.com/?FunctionalProgrammingLa
nguage
Algorithms in Functional Programming:
Sorting examples
https://smthngsmwhr.wordpress.com/2012/11
/09/sorting-algorithms-in-haskell/
http://stackoverflow.com/questions/18761766
/mergesort-python
Data-Mining in Functional Programming:
http://www.anderson.ucla.edu/faculty/jason.fr
and/teacher/technologies/palace/datamining.
htm
https://www.fpcomplete.com/blog/2016/09/dat
a-haskell
http://stevenskelton.ca/real-time-data-mining-
spark/
http://www.kdnuggets.com/2012/08/poll-analy
tics-data-mining-programming-languages.htm
l
Data-Visualization in Functional
Programming:
A Survey of Program Visualization for the
Gaming References:
“Top 7 Programming Languages Used in
Video Games”
https://www.freelancer.com/community/articl
es/top-7-programming-languages-used-in-vi
deo-games
“FRP and Yampa Tutorials”
http://www.cs.nott.ac.uk/~nhn/frp-yampa-tut
orials.html
“A breakout game in Haskell using SDL and
FRP”
https://github.com/ivanperez-keera/haskhan
oid
“From 60 Frames per Second to 500 in
Haskell”
http://keera.co.uk/blog/2014/10/15/from-60-f
ps-to-500/
“Functional reactive programming”
https://en.wikipedia.org/wiki/Functional_react
ive_programming
“The Yampa package”
http://hackage.haskell.org/package/Yampa-0
.9.6
“Types, Again”
https://www.haskell.org/tutorial/moretypes.ht
ml
Critique
http://nsr.oxfordjournals.org/content/2/3/349.
full
Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
16
Functional Paradigm
http://staff.elka.pw.edu.pl/~mszlenk/pdf/Meta
model-UML-Profile-FPL.pdf
Metamodel and UML Profile for Functional
Programming Languages
http://www.dcs.warwick.ac.uk/pvw04/p01.pdf
Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016

Mais conteúdo relacionado

Mais procurados

A short tutorial on r
A short tutorial on rA short tutorial on r
A short tutorial on rAshraf Uddin
 
Introduction to the language R
Introduction to the language RIntroduction to the language R
Introduction to the language Rfbenault
 
R programming groundup-basic-section-i
R programming groundup-basic-section-iR programming groundup-basic-section-i
R programming groundup-basic-section-iDr. Awase Khirni Syed
 
R programming slides
R  programming slidesR  programming slides
R programming slidesPankaj Saini
 
R programming for data science
R programming for data scienceR programming for data science
R programming for data scienceSovello Hildebrand
 
Functional Programming in R
Functional Programming in RFunctional Programming in R
Functional Programming in RSoumendra Dhanee
 
R programming Language , Rahul Singh
R programming Language , Rahul SinghR programming Language , Rahul Singh
R programming Language , Rahul SinghRavi Basil
 
RDataMining slides-r-programming
RDataMining slides-r-programmingRDataMining slides-r-programming
RDataMining slides-r-programmingYanchang Zhao
 
Introduction to data analysis using R
Introduction to data analysis using RIntroduction to data analysis using R
Introduction to data analysis using RVictoria López
 
Introduction to Rstudio
Introduction to RstudioIntroduction to Rstudio
Introduction to RstudioOlga Scrivner
 
R programming presentation
R programming presentationR programming presentation
R programming presentationAkshat Sharma
 
R language tutorial
R language tutorialR language tutorial
R language tutorialDavid Chiu
 

Mais procurados (20)

A short tutorial on r
A short tutorial on rA short tutorial on r
A short tutorial on r
 
Introduction to the language R
Introduction to the language RIntroduction to the language R
Introduction to the language R
 
LSESU a Taste of R Language Workshop
LSESU a Taste of R Language WorkshopLSESU a Taste of R Language Workshop
LSESU a Taste of R Language Workshop
 
R programming groundup-basic-section-i
R programming groundup-basic-section-iR programming groundup-basic-section-i
R programming groundup-basic-section-i
 
Basic lisp
Basic lispBasic lisp
Basic lisp
 
R programming slides
R  programming slidesR  programming slides
R programming slides
 
R programming for data science
R programming for data scienceR programming for data science
R programming for data science
 
Functional Programming in R
Functional Programming in RFunctional Programming in R
Functional Programming in R
 
R programming
R programmingR programming
R programming
 
R programming
R programmingR programming
R programming
 
R programming Language , Rahul Singh
R programming Language , Rahul SinghR programming Language , Rahul Singh
R programming Language , Rahul Singh
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
R language
R languageR language
R language
 
R programming
R programmingR programming
R programming
 
RDataMining slides-r-programming
RDataMining slides-r-programmingRDataMining slides-r-programming
RDataMining slides-r-programming
 
Introduction to data analysis using R
Introduction to data analysis using RIntroduction to data analysis using R
Introduction to data analysis using R
 
Introduction to Rstudio
Introduction to RstudioIntroduction to Rstudio
Introduction to Rstudio
 
R programming presentation
R programming presentationR programming presentation
R programming presentation
 
R language tutorial
R language tutorialR language tutorial
R language tutorial
 
R language
R languageR language
R language
 

Semelhante a Programming Languages - Functional Programming Paper

1_Introduction.pptx
1_Introduction.pptx1_Introduction.pptx
1_Introduction.pptxranapoonam1
 
R basics for MBA Students[1].pptx
R basics for MBA Students[1].pptxR basics for MBA Students[1].pptx
R basics for MBA Students[1].pptxrajalakshmi5921
 
Modern Programming Languages classification Poster
Modern Programming Languages classification PosterModern Programming Languages classification Poster
Modern Programming Languages classification PosterSaulo Aguiar
 
Intellectual technologies
Intellectual technologiesIntellectual technologies
Intellectual technologiesPolad Saruxanov
 
Using Language Oriented Programming to Execute Computations on the GPU
Using Language Oriented Programming to Execute Computations on the GPUUsing Language Oriented Programming to Execute Computations on the GPU
Using Language Oriented Programming to Execute Computations on the GPUSkills Matter
 
Functional Programming.pptx
Functional Programming.pptxFunctional Programming.pptx
Functional Programming.pptxKarthickT28
 
Programming in Scala - Lecture One
Programming in Scala - Lecture OneProgramming in Scala - Lecture One
Programming in Scala - Lecture OneAngelo Corsaro
 
Concepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming LanguagesConcepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming Languagesppd1961
 
PL Lecture 01 - preliminaries
PL Lecture 01 - preliminariesPL Lecture 01 - preliminaries
PL Lecture 01 - preliminariesSchwannden Kuo
 
Unit1 principle of programming language
Unit1 principle of programming languageUnit1 principle of programming language
Unit1 principle of programming languageVasavi College of Engg
 
Parsl: Pervasive Parallel Programming in Python
Parsl: Pervasive Parallel Programming in PythonParsl: Pervasive Parallel Programming in Python
Parsl: Pervasive Parallel Programming in PythonDaniel S. Katz
 
CSCorganization of programming languages
CSCorganization of programming languagesCSCorganization of programming languages
CSCorganization of programming languagesOluwafolakeOjo
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture TwoAngelo Corsaro
 
Scaling Application on High Performance Computing Clusters and Analysis of th...
Scaling Application on High Performance Computing Clusters and Analysis of th...Scaling Application on High Performance Computing Clusters and Analysis of th...
Scaling Application on High Performance Computing Clusters and Analysis of th...Rusif Eyvazli
 

Semelhante a Programming Languages - Functional Programming Paper (20)

1_Introduction.pptx
1_Introduction.pptx1_Introduction.pptx
1_Introduction.pptx
 
R basics for MBA Students[1].pptx
R basics for MBA Students[1].pptxR basics for MBA Students[1].pptx
R basics for MBA Students[1].pptx
 
Modern Programming Languages classification Poster
Modern Programming Languages classification PosterModern Programming Languages classification Poster
Modern Programming Languages classification Poster
 
Intellectual technologies
Intellectual technologiesIntellectual technologies
Intellectual technologies
 
Presentation1
Presentation1Presentation1
Presentation1
 
Using Language Oriented Programming to Execute Computations on the GPU
Using Language Oriented Programming to Execute Computations on the GPUUsing Language Oriented Programming to Execute Computations on the GPU
Using Language Oriented Programming to Execute Computations on the GPU
 
Inroduction to r
Inroduction to rInroduction to r
Inroduction to r
 
Special topics in finance lecture 2
Special topics in finance   lecture 2Special topics in finance   lecture 2
Special topics in finance lecture 2
 
Functional Programming.pptx
Functional Programming.pptxFunctional Programming.pptx
Functional Programming.pptx
 
Programming in Scala - Lecture One
Programming in Scala - Lecture OneProgramming in Scala - Lecture One
Programming in Scala - Lecture One
 
PARADIGM IT.pptx
PARADIGM IT.pptxPARADIGM IT.pptx
PARADIGM IT.pptx
 
R programming Language
R programming LanguageR programming Language
R programming Language
 
Concepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming LanguagesConcepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming Languages
 
PL Lecture 01 - preliminaries
PL Lecture 01 - preliminariesPL Lecture 01 - preliminaries
PL Lecture 01 - preliminaries
 
Unit1 principle of programming language
Unit1 principle of programming languageUnit1 principle of programming language
Unit1 principle of programming language
 
Parsl: Pervasive Parallel Programming in Python
Parsl: Pervasive Parallel Programming in PythonParsl: Pervasive Parallel Programming in Python
Parsl: Pervasive Parallel Programming in Python
 
CSCorganization of programming languages
CSCorganization of programming languagesCSCorganization of programming languages
CSCorganization of programming languages
 
R_L1-Aug-2022.pptx
R_L1-Aug-2022.pptxR_L1-Aug-2022.pptx
R_L1-Aug-2022.pptx
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture Two
 
Scaling Application on High Performance Computing Clusters and Analysis of th...
Scaling Application on High Performance Computing Clusters and Analysis of th...Scaling Application on High Performance Computing Clusters and Analysis of th...
Scaling Application on High Performance Computing Clusters and Analysis of th...
 

Mais de Shreya Chakrabarti

Certificate in google analytics beginners
Certificate in google analytics   beginnersCertificate in google analytics   beginners
Certificate in google analytics beginnersShreya Chakrabarti
 
Citizen Data Scientist : Marketing perspective Certification
Citizen Data Scientist : Marketing perspective CertificationCitizen Data Scientist : Marketing perspective Certification
Citizen Data Scientist : Marketing perspective CertificationShreya Chakrabarti
 
Microsoft Virtual Academy Certificate of Completion Python
Microsoft Virtual Academy Certificate of Completion PythonMicrosoft Virtual Academy Certificate of Completion Python
Microsoft Virtual Academy Certificate of Completion PythonShreya Chakrabarti
 
Intelligent Systems - Predictive Analytics Project
Intelligent Systems - Predictive Analytics ProjectIntelligent Systems - Predictive Analytics Project
Intelligent Systems - Predictive Analytics ProjectShreya Chakrabarti
 
Survey Paper on Google Project Loon- Ballon for Everyone
Survey Paper on Google Project Loon- Ballon for EveryoneSurvey Paper on Google Project Loon- Ballon for Everyone
Survey Paper on Google Project Loon- Ballon for EveryoneShreya Chakrabarti
 
Summer Independent Study Report
Summer Independent Study ReportSummer Independent Study Report
Summer Independent Study ReportShreya Chakrabarti
 

Mais de Shreya Chakrabarti (11)

Certificate in google analytics beginners
Certificate in google analytics   beginnersCertificate in google analytics   beginners
Certificate in google analytics beginners
 
Citizen Data Scientist : Marketing perspective Certification
Citizen Data Scientist : Marketing perspective CertificationCitizen Data Scientist : Marketing perspective Certification
Citizen Data Scientist : Marketing perspective Certification
 
Machine Learning with MATLAB
Machine Learning with MATLABMachine Learning with MATLAB
Machine Learning with MATLAB
 
Microsoft Virtual Academy Certificate of Completion Python
Microsoft Virtual Academy Certificate of Completion PythonMicrosoft Virtual Academy Certificate of Completion Python
Microsoft Virtual Academy Certificate of Completion Python
 
Intelligent Systems - Predictive Analytics Project
Intelligent Systems - Predictive Analytics ProjectIntelligent Systems - Predictive Analytics Project
Intelligent Systems - Predictive Analytics Project
 
PROJECT PPT
PROJECT PPTPROJECT PPT
PROJECT PPT
 
BE Project
BE ProjectBE Project
BE Project
 
Project Loon - Final PPT
Project Loon - Final PPTProject Loon - Final PPT
Project Loon - Final PPT
 
Survey Paper on Google Project Loon- Ballon for Everyone
Survey Paper on Google Project Loon- Ballon for EveryoneSurvey Paper on Google Project Loon- Ballon for Everyone
Survey Paper on Google Project Loon- Ballon for Everyone
 
BICFinal06
BICFinal06BICFinal06
BICFinal06
 
Summer Independent Study Report
Summer Independent Study ReportSummer Independent Study Report
Summer Independent Study Report
 

Programming Languages - Functional Programming Paper

  • 1. Functional Programming Submitted by: Chad McQuinn,Nicholas Armstrong,Shreyas C.S & Shreya Chakrabarti Abstract In the project, we have studied, applications that can use functional programming languages. We have studied the numerous approaches for using Functional Programming languages in applications like Algorithms,Data-Mining,Game Programming and Data Visualization. In this report we are going to discuss those applications on the basis of performance, understandability, readability etc. This report contains the information about these applications in various functional languages and the analysis that we have performed on those languages. Rajeev Raje | CS 56500 - Programming Languages | Project Report | 12/12/2016
  • 2. 1 Table of Contents Table of Cont​ents Introduction to Functional Programming Languages and Problem Statement 2 Proposed Approach and Outcomes 2 Applications of Functional Programming Languages 2 Algorithms in Functional Programming 3 Data Mining in Functional Programming 5 Game Programming in Haskell 6 Data Visualization 7 Observations 8 Conclusions 9 Critique 9 Appendix 10 Functional Programming | References 1​4 Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
  • 3. 2 Introduction to Functional Programming Languages and Problem Statement The main motivation behind developing a Functional Programming paradigm was to make the behaviour of a program predictable.Functional programming was thus developed based on Mathematics namely to maintain its predictable nature. Functional Programming language finds its roots in “Lambda Calculus” and as indicated in the name itself Functional Programming languages operates using functions.Due to its inherent property of avoiding changing state and mutable data function,functions in these type of programming languages provide same output every-time the same function is called. Although there are many more programming languages based on functional programming, some of the most popular pure functional languages which find applications in the real world include Haskell, Hope, Erlang, Clojure, and Common Lisp. Our problem is to use some of these languages to study their implementation in some of the popular applications in computer science.These applications were chosen as the demand for computations in some of these areas is higher than others and application of languages which use a functional paradigm and are mathematical in nature would be of greater use in this area. Proposed Approach and Outcomes We will examine functional approaches to solving various problems in computer science and software engineering. Our selected problems are Algorithms (in particular, sorting), Data Mining, Game Programming, and Data Visualization. In each section, we will describe how well functional programming fits the problem, using measures such as code simplicity, performance (time/space complexity), popularity, and other factors. Applications of Functional Programming Languages The various areas of Computer Science which we thought would benefit from the inherent properties of Functional Programming languages are discussed in the following section. Fig 1 is a pictorial representation of the properties of Functional Programming Languages that we found might be helpful in the applications discussed after the figure. Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
  • 4. 3 ​ Fig:1 Algorithms in Functional Programming Algorithms boast of being the heart of computer science field. Problem solving using Algorithms has found widespread application in areas beyond Computer Science as well.We tried to implement a merge sort algorithm in a Functional and Non-Functional Programming Language. Consider the example sorting- Mergesort algorithm in Python and Haskell: Python Haskell 1. List is split into two parts def merge(a, b): if len(a) == 0: return b elif len(b) == 0: return a elif a[0] < b[0]: return [a[0]] + merge(a[1:], b) else: 1. List is split into two parts mergesort'splitinhalf :: [a] -> ([a], [a]) mergesort'splitinhalf xs = (take n xs, drop n xs) where n = (length xs) `div` 2 Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
  • 5. 4 return [b[0]] + merge(a, b[1:]) 2. Two parts are sorted by the algorithm 3. The sorted parts are merged by a special merging procedure for sorted lists def mergesort(x): if len(x) < 2: return x else: h = len(x) // 2 return merge(mergesort(x[:h]), mergesort(x[h:])) 2. Two parts are sorted by the algorithm mergesort'merge :: (Ord a) => [a] -> [a] -> [a] mergesort'merge [] xs = xs mergesort'merge xs [] = xs mergesort'merge (x:xs) (y:ys) | (x < y) = x:mergesort'merge xs (y:ys) | otherwise = y:mergesort'merge (x:xs) ys 3. The sorted parts are merged by a special merging procedure for sorted lists mergesort :: (Ord a) => [a] -> [a] mergesort xs | (length xs) > 1 = mergesort'merge (mergesort ls) (mergesort rs) | otherwise = xs where (ls, rs) = mergesort'splitinhalf xs About Python syntax: ● L[0] is the first element of list L ● L[1:] is the list of all remaining elements ● More generally L[:n] is the list of up to the n-th element, L[n:] the rest ● A + B if A and B are both lists is the list obtained by concatenation ● [x] is a list containing just the single element x About Haskell syntax: mergesort’splitinhalf : returns a pair of arrays into which the original array was split. n is equal to the half of the length of the array, and then we use the standard functions ​take​ and ​drop​. take n xs ​: Get the first n elements of the list drop n xs : rest of the list after those first n elements Function for merging two sorted arrays: The function receives two arrays and produces one array of the same type. The algorithm for merging: 1. If the first list is empty [] then the result of the merge is the second list xs 2. If the second list is empty [] then the result of the merge is the first list xs Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
  • 6. 5 3. Otherwise we compare the first elements of the lists and append with the colon : function the least of them to the new list which is the result of merging the remaining two lists Function mergesort: If the length of the list is greater than 1 then we do the standard step of the algorithm. Otherwise the list with the length of 1 is already sorted (the condition for ending the recursion). The code reads exactly the same as the textual description of the algorithm given earlier but now this is a formal and shorter specification. Data Mining in Functional Programming With the increasing advent of Data being used in nearly every possible field in the real world. Data driven decision making and improving performances of tools already in place to operate on this data is becoming vital with every passing day. Functional Programming language was thought to find a great application in the field because of its strengths of Parallelism,Transformations and Ability to perform faster computations. We used an example in Non-Functional,Functional and a hybrid language to make observations of data mining tasks done in these respective languages. Code for these examples is mentioned in the appendix. Parameters Data Mining in Non-Functional Language (Python) Data Mining in Functional Language (Haskell) Data Mining in Hybrid Language (Scala) Level of Difficulty to learn Easy Difficult Moderate Orientation Analytical Oriented Language Mathematically oriented Language Engineering Oriented Language Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
  • 7. 6 Data Parsing One data type can be easily parsed as other data type The fields are statically typed and it cannot be passed as any other data type,if tried it would raise a compile error Data parsing although not inherent to scala it uses XML packages for data parsing Memory Usage The entire data file is loaded into memory Data is achieved in streaming fashion, with constant memory usage Data file is loaded into memory Calculations Functions need to be written to perform further advanced calculations on the data Inherent Mathematical features make it very easy to perform advanced calculations Inherent Functional Programming features allow faster computations in Scala There was no significant difference found in execution times for examples run in the above languages.The poll in the appendix of this paper, and other resources on the internet show that using functional programming languages despite their mathematical abilities are not a popular choice amongst practitioners of Data Mining in the real world. Other Data Mining Tools based on Functional Programming Languages ● Common Lisp ● Clojure (Dialect of Common Lisp) ● Julia- Although not functional because of its ability to support extensive mathematical function library it is considered functional many a times ● PolyFARM - Data Mining application developed entirely in Standard Haskell Game Programming in Haskell Not many commercial games have been written in Haskell (see references). Henrik Nilsson & Ivan Perez presented at PDP14 (Principles and Practice of Declarative Programming) on a breakout game written in Haskell as a proof of concept. Their premise was to use features of declarative functional programming to view games as a “network of signals and signal transformers”. Their proof of concept included multiple levels and even background music. Ultimately this was expanded into a full Android game. These authors were involved with Keera Studios and have published a blog describing optimizing a game that was producing 10-15 FPS (frames per second) on Android and 60-70 FPS (frames per second) on desktop machines. Ultimately through optimization techniques, they achieved 500 FPS on desktop machines. Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
  • 8. 7 After realizing that traditional functional programming was in some ways not a good fit for game programming, they turned to Functional Reactive Programming (FRP), which allows for a functional language to describe entities that change over time. These changes can be discrete or continuous, and the authors found a Haskell package, Yampa, that supports both models (4). Their first approach was to profile the application. They were examining memory usage, speed (primarily measured in FPS), and impact of the changes (simplicity of the required code). Time and Space Complexity They first determined where time was being spent and how much memory was being consumed. Initially some optimizations were applied to native libraries being used by the game, but this resulted in a relatively small increase of 20-30 FPS. They next found that Haskell implementation was consuming hundreds of megabytes of memory. Worse, memory usage was growing dramatically over time. This was largely due to lazy evaluation. Since individual object fields are not evaluated until they are needed, an entire object must live until this evaluation is performed--even if it is ultimately never needed. This resulted in a large amount of memory growth over time. By using ​strictness(6) (which overcomes the lazy evaluation) they were able to overcome this. This reduced memory consumption and increased FPS dramatically. The desktop version increased from 90 FPS to over 300 FPS with this change. Memory usage also exhibited a flat profile with a steady usage of around 3 megabytes. Here is an example of strictness in Haskell: the language’s ​Complex data type data RealFloat a => Complex a = !a :+ !a This definition forces both components to be evaluated at definition time; a Complex instance will not live on simply because (for example) its imaginary component has not been evaluated yet. Simplicity of Code The authors describe code changes to achieve parallelization that in many cases were extremely simple--as few as 5 or 10 lines of code increasing parallelization and consequently FPS by 10% or more. Some easy parallelization increased the desktop version to over 500 FPS. However, Android performance had only been increased to around 25 FPS. At this framerate, collisions were not precise and the physics of the game were not realistic, with objects passing through each other, etc. By isolating the physics logic from the renderer and parallelizing this way, they were able to achieve physics evaluation at over 30 FPS while the renderer stayed at 15 FPS. This resulted in much more satisfactory physics behavior. This parallelization for Android required just a few new objects and 10 lines of code. Data Visualization One of the key issues with visualising data in functional program involves its inherent tree like structure. As mentioned in the “A Survey of Program Visualizations for the Functional Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
  • 9. 8 Paradigm” there are situations involving lazy evaluation where these trees become directed graphs. Also noted there are several utilities that handle this visualization including CIDER, KIEL, Prospero, Hint, and TERSE. Dealing with large trees or directed graphs has its own concerns so several of these utilities provide ways of compressing these graphs. Some utilities provide filters for visualizing data in the spatial sense and temporal sense. Some of these utilities reduce the tree(graph) by minimizing regions that are trivial evaluations. One thing that is noticeable about these utilities is that it attaches itself to a paradigm that is quite commonly learned in intro computer science classes. The flow chart. Another aspect to consider is UML. As noted in “Metamodel and UML Profile for Functional Programming Languages” UML was originally intended to be a universal general purpose modeling language. Applying UML to functional programming requires some modification. One of those modifications ties into the functional language Haskell referred to as the Haskell metamodel. The metamodel consists of three class diagrams. Haskell programs are organized into modules. Two program elements are represented in a module, functions and user data types. Observations 1. ​Higher-order functions​ (which let a person pass functions to functions, or return functions from functions) let a person factor out a lot of duplication in the small. In algorithms section we refactored the existing Java app to use higher order functions, and found and fixed several bugs along the way (all related to bugs by copy & paste). 2. ​Immutable data structures​, which are often used in FP, free an individual from having to constantly worry about what the code will do to the data which is passed into it. In the Java app, we found a lot of “defensive copying”, which could simply be deleted as we changed many core data structures from mutable to immutable. 3. ​Strong types​, which appear in many functional programming languages (but not all), tell us more about statically-proven properties of the code. In the Java app, we changed a lot of code from using null to using a generic optional data structure, which more clearly communicates where values may be absent. This let us delete a lot of defensive null-checking, as well as fix a few NPEs in uncommon code paths. 4. ​Pure functions​, which are functions that don’t have side effects (i.e. their output is a deterministic function of their input), are much easier to understand and test, because one doesn’t have to wonder if the function’s behavior will change based on hidden state. In the Java application, we were able to convert a lot of stateful functions to stateless functions, which made the code much simpler and fixed a few bugs. Some of the reasons we found out,why functional programming languages are not a popular choice amongst people from all of these areas are listed as below: ● Functional languages forbid state and traditional iteration structures Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
  • 10. 9 ● Absent ​side effects, all computation is done by evaluating functions, and these functions should behave the same way any time they are called with precisely the same arguments ● Functional programs forbid assignment ● All loops must be implemented with recursion. This might seem absurdly restrictive ● Difficult to learn Conclusions Although we tried to find topics that played to the strengths of functional programming, we did not find many real-world uses of pure functional programming. In Data mining, we found that hybrid languages(Scala) are much more popular. Some of the most common computer science algorithms do not lend themselves to a pure functional model. Although the authors we surveyed showed that you can write a game using pure functional programming, this still isn’t a popular choice. The relative lack of popularity of pure functional programming has also led to there being few tools for visualization. Critique Goals of Software Engineering: 1. Code that works reliably, even in parts of the application that aren’t used very often. 2. Code that can be easily understood by other people (The person writing the code won’t be around forever). 3. Code that is factored in such a way as to minimize the cost of changing it (because if one thing is certain, it’s that requirements never stop changing). (They also usually want it fast and cheap, too, but that’s a topic for another post.) For our algorithms section, despite the fact that this program is “purely functional”, the code is complete and utter garbage: 1. It had several bugs when first written, and it took a lot of time to track them down. 2. It’s hard to understand. In fact, a C implementation would probably be easier to understand. 3. For such a small function, it would be awfully hard to maintain. Making changes safely would require a large amount of mental effort and testing, and you might not be able to reuse much code. More generally, our original goal was to survey more topics.Lack of information in many areas, lead us on including only the four in the paper. We also wanted to find use cases for which functional programming was a good fit, a poor fit, or somewhere in-between, but this judgment was very subjective and we dropped it. Functional languages are increasingly being adopted by industry in the real world. To name a few examples, Haskell used by Facebook makes its news feeds run smoothly; WhatsApp relies on Erlang to run messaging servers, achieving up to 2 million connected users per server; Twitter, LinkedIn, Foursquare, Tumblr, and Klout rely on the hybrid, Scala to build Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
  • 11. 10 their core infrastructure for sites. Indirect application of functional programming features include Google’s popular Map-Reduce algorithm which derives its map and reduce functions commonly found in functional programming. What would we do if we were starting over today? We would probably try to do more comparison of functional programming against other paradigms, and do more presentation of measurable results. Appendix Data Mining Poll Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
  • 12. 11 (http://www.kdnuggets.com/2012/08/poll-analytics-data-mining-programming-languages.html) Code for Data Mining 1) Code Snippet for Data Mining in Python import zipfile import requests import numpy as np import pandas as pd Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
  • 13. 12 import seaborn as sns import matplotlib.pyplot as plt r = requests.get('https://github.com/datailluminations/PredictingFlightsDelay', stream=True) with open("flights.csv", 'wb') as f: for chunk in r.iter_content(chunk_size=1024): if chunk: f.write(chunk) zf = zipfile.ZipFile("flights.csv.zip") filename = zf.filelist[0].filename fp = zf.extract(filename) df = pd.read_csv(fp, parse_dates="FL_DATE").rename(columns=str.lower) 2) ​Code Snippet for Data Mining in Haskell {-# LANGUAGE TypeApplications, OverloadedStrings, OverloadedLabels, TypeOperators, DataKinds, FlexibleContexts #-} import Labels.Explore main = runResourceT $ httpSource "https://github.com/datailluminations/PredictingFlightsDelay" responseBody .| zipEntryConduit "ontime.csv" .| fromCsvConduit @("fl_date" := Day, "tail_num" := String) (set #downcase True csv) .| dropConduit 10 .| takeConduit 5 .> tableSink 3) ​Code Snippet for Data Mining in Scala import java.io.File import scala.io.{ BufferedSource, Source } import sys.process._ import java.net.URL import java.io.File new URL("https://github.com/datailluminations/PredictingFlightsDelay") #> new File("Output.html") abstract class StackTable[T] { Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
  • 14. 13 val file: File def getDate(n: scala.xml.NodeSeq): Long = n.text match { case "" => 0 case s => dateFormat.parse(s).getTime } def dateFormat = { import java.text.SimpleDateFormat import java.util.TimeZone val f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS") f.setTimeZone(TimeZone.getTimeZone("GMT")) f } def getInt(n: scala.xml.NodeSeq): Int = n.text match { case "" => 0 case x => x.toInt } def parseXml(x: scala.xml.Elem): T def parse(s: String): Option[T] = if (s.startsWith(" <row ")) Some(parseXml(scala.xml.XML.loadString(s))) else None } Code for Merge sort 1) Code Snippet for Merge sort in Haskell mergesort'splitinhalf :: [a] -> ([a], [a]) mergesort'splitinhalf xs = (take n xs, drop n xs) where n = (length xs) `div` 2 mergesort'merge :: (Ord a) => [a] -> [a] -> [a] mergesort'merge [] xs = xs mergesort'merge xs [] = xs mergesort'merge (x:xs) (y:ys) | (x < y) = x:mergesort'merge xs (y:ys) | otherwise = y:mergesort'merge (x:xs) ys mergesort :: (Ord a) => [a] -> [a] mergesort xs | (length xs) > 1 = mergesort'merge (mergesort ls) (mergesort rs) | otherwise = xs where (ls, rs) = mergesort'splitinhalf xs Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
  • 15. 14 2) Code Snippet for Merge sort in Python def merge(a, b): if len(a) == 0: return b elif len(b) == 0: return a elif a[0] < b[0]: return [a[0]] + merge(a[1:], b) else: return [b[0]] + merge(a, b[1:]) def mergesort(x): if len(x) < 2: return x else: h = len(x) // 2 return merge(mergesort(x[:h]), mergesort(x[h:])) Functional Programming | References Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
  • 16. 15 Image on Report Title: http://programminghints.com/2016/04/need-fu nctional-programming/ Introduction to Functional Programming: https://en.wikipedia.org/wiki/Functional_progr amming http://comjnl.oxfordjournals.org/content/32/2/ 98.short http://wiki.c2.com/?FunctionalProgrammingLa nguage Algorithms in Functional Programming: Sorting examples https://smthngsmwhr.wordpress.com/2012/11 /09/sorting-algorithms-in-haskell/ http://stackoverflow.com/questions/18761766 /mergesort-python Data-Mining in Functional Programming: http://www.anderson.ucla.edu/faculty/jason.fr and/teacher/technologies/palace/datamining. htm https://www.fpcomplete.com/blog/2016/09/dat a-haskell http://stevenskelton.ca/real-time-data-mining- spark/ http://www.kdnuggets.com/2012/08/poll-analy tics-data-mining-programming-languages.htm l Data-Visualization in Functional Programming: A Survey of Program Visualization for the Gaming References: “Top 7 Programming Languages Used in Video Games” https://www.freelancer.com/community/articl es/top-7-programming-languages-used-in-vi deo-games “FRP and Yampa Tutorials” http://www.cs.nott.ac.uk/~nhn/frp-yampa-tut orials.html “A breakout game in Haskell using SDL and FRP” https://github.com/ivanperez-keera/haskhan oid “From 60 Frames per Second to 500 in Haskell” http://keera.co.uk/blog/2014/10/15/from-60-f ps-to-500/ “Functional reactive programming” https://en.wikipedia.org/wiki/Functional_react ive_programming “The Yampa package” http://hackage.haskell.org/package/Yampa-0 .9.6 “Types, Again” https://www.haskell.org/tutorial/moretypes.ht ml Critique http://nsr.oxfordjournals.org/content/2/3/349. full Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016
  • 17. 16 Functional Paradigm http://staff.elka.pw.edu.pl/~mszlenk/pdf/Meta model-UML-Profile-FPL.pdf Metamodel and UML Profile for Functional Programming Languages http://www.dcs.warwick.ac.uk/pvw04/p01.pdf Functional Programming | CS 56500 - Programming Languages | Project Report | 12/12/2016