Presentation is loading. Please wait.

Presentation is loading. Please wait.

HASKELL Presentation Programming Segment

Similar presentations


Presentation on theme: "HASKELL Presentation Programming Segment"— Presentation transcript:

1 HASKELL Presentation Programming Segment
Anthony Leach

2 Calculate roots quadratic eq.
Input parameter -- function definition roots :: (Float, Float, Float) -> (Float, Float) -- Funct. concrete statements //code provide by roots (a, b, c) = if d < 0 then error "undefined" else (x1, x2) where x1 = e + sqrt d / (2 * a) x2 = e - sqrt d / (2 * a) d = b * b - 4 * a * c e = -b / (2 * a) -- passing parameters p1, p2, p3, p4 :: (Float, Float, Float) -- Testing diff. values for a, b, c p1 = (1.0, 5.0, 1.0) p2 = (1.0, 1.0, 1.0) p3 = (-2.0, 4.0, 1.0) p4 = (1.0, 14.0, -2.0) Output parameter Function name

3 Running quad.hs

4 Comparison of Haskell and C, C++
C or C++ // header file #include stdio.h //Function definition void roots( float, float, float ); //implement file //concrete statements void roots (float a, float b, float c) { ……………… } //main.c int main() roots(a, b, c); HASKELL -- function definition roots :: (Float, Float, Float) -> (Float, Float) --concrete statements roots (a, b, c) = (x1, x2) where x1 = e + sqrt d / (2 * a) x2 = e - sqrt d / (2 * a) d = b * b - 4 * a * c e = -b / (2 * a) Readable Powerful No or low setup time for programming environment

5 Comparison of Haskell, C, Scheme
Powerful Efficient Quicksort in Haskell code provide by haskell.org qsort [] = [] qsort (x:xs) = qsort elts_lt_x ++ [x] ++ qsort elts_greq_x where elts_lt_x = [y | y <- xs, y < x] elts_greq_x = [y | y <- xs, y >= x] Quicksort in Scheme (define quicksort (lambda (ls) (if (null? ls) '() (let* ((pivot (car ls)) (rest (cdr ls)) (left (partition < pivot rest)) (right (partition >= pivot rest))) (append (quicksort left) (list pivot) (quicksort right)))))) Quicksort in C qsort( a, lo, hi ) int a[], hi, lo; { int h, l, p, t; if (lo < hi) { l = lo; h = hi; p = a[hi]; do { while ((l < h) && (a[l] <= p)) l = l+1; while ((h > l) && (a[h] >= p)) h = h-1; if (l < h) { t = a[l]; a[l] = a[h]; a[h] = t; } …………………………….

6 Final Notes Haskell is a very high-level language (many details taken care of automatically). Haskell is expressive and concise (can achieve a lot with a little effort). Haskell is good at handling complex data and combining components.


Download ppt "HASKELL Presentation Programming Segment"

Similar presentations


Ads by Google