# Haskell & Functional

Post on 31-Dec-2015

27 views

Category:

## Documents

Embed Size (px)

DESCRIPTION

Haskell & Functional. Peeking functional programming. . , , C, Pascal, C++, Java, Prolog, Perl, Python, . . : . . . : - PowerPoint PPT Presentation

TRANSCRIPT

• Haskell& FunctionalPeeking functional programming

P L U S

, , C, Pascal, C++, Java, Prolog, Perl, Python,

P L U S

: . . . : C .

P L U S

? . , , . . , .

P L U S

? ?

.Functional programming .

P L U S

Functional Programming? . , . , . ( ) .

P L U S

. , . .

P L U S

( 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] l) && (a[h] >= p)) h = h-1; if (l < h) { t = a[l]; a[l] = a[h]; a[h] = t; } } while (l < h); t = a[l]; a[l] = a[hi]; a[hi] = t;

qsort( a, lo, l-1 ); qsort( a, l+1, hi ); }}

P L U S

( in Haskell )qsort [] = []qsort (x:xs) = qsort lt_x ++ [x] ++ qsort greq_xwherelt_x = [y | y IntsquareInt n = n*n

difSquare x y = (x-y)^2-- polymorphic type

f = \x -> 2 * n

P L U S

Recursionfac1 :: Int -> Intfac1 n = if n==1 then 1 else (n * fac (n-1))

fac2 :: Int -> Intfac2 n = prodList [1 .. n ]prodList lt = if (length h)==1 then head ltelse head lt * (prodList (tail lt))

P L U S

Pattern matchingprodLst2 [] = 0prodLst2 [x] = xprodLst2 (x:xs) = x * prodLst2 xs

isSubseq [] _ = TrueisSubseq _ [] = FalseisSubseq lt (x:xs) = (lt==start) || isSubseq lt xswhere start = take (length lt) (x:xs)

P L U S

GuardsprodLst3 lst | length lst==0 = 0| length lst==1 = head lst| otherwise = head lst * prodLst3 (tail lst)

isSublist [] _ = TrueisSublist _ [] = FalseisSublist (e:es) (x:xs)| e==x && isSublist es xs = True| otherwise = isSublist (e:es) xs

P L U S

List comprehensionsmyLst :: [(Int,Int,Int)]myLst = [(i,j,i*j) | i