1 / 21

Haskell & Functional

Haskell & Functional. Peeking functional programming. 프로그래밍 언어의 변화. 기계 중심 언어와 인간 중심 언어 기계어 , 어셈블러 , … C, Pascal, … C++, Java, … Prolog, Perl, Python, …. 프로그래밍 언어의 변화. 주 프로그래밍 언어의 변화 이전 : 기계 중심 언어가 주로 사용되었다 . 컴퓨터의 성능이 좋지 않았다 . 고급 개념의 컴퓨터 구현이 부족했다 . 현대 : 점점 인간 중심 언어로 이동

Télécharger la présentation

Haskell & Functional

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Haskell& Functional Peeking functional programming

  2. 프로그래밍 언어의 변화 • 기계 중심 언어와 인간 중심 언어 • 기계어, 어셈블러, … • C, Pascal, … • C++, Java, … • Prolog, Perl, Python, …

  3. 프로그래밍 언어의 변화 • 주 프로그래밍 언어의 변화 • 이전 : 기계 중심 언어가 주로 사용되었다. • 컴퓨터의 성능이 좋지 않았다. • 고급 개념의 컴퓨터 구현이 부족했다. • 현대 : 점점 인간 중심 언어로 이동 • C와 같은 기계 중심 언어에서 인간 중심 언어로 주 사용 언어가 이동하고 있다.

  4. 인간 중심 언어? • 기계 중심 언어 • 기계 중심으로 사고해야 한다. • 프로그램 제작, 유지, 보수가 어렵다. • 인간 중심 언어 • 인간의 사고 방식에 가까운 언어이다. • 에러가 적고, 코드를 읽기가 쉽다.

  5. 인간의 사고 방식? • 사람이 생각하는 것을 어떻게 하면 명확하게 표현할 수 있을까? • 수학적 표기 방법을 이용한다. • Functional programming 은 수학적인 표현 방법과 유사하다.

  6. Functional Programming? • 함수가 특별하지 않다. • 계산을 어떻게 하는 지가 아니라, 무엇을 계산하는지에 초점이 맞춰져 있다. • 프로그램은 명령의 나열이 아니라, 값을 계산하기 위한 계산식이다. • 기계적인 부분(메모리 관리 등)에 신경 쓸 필요가 없다.

  7. 퀵 소트 알고리즘 • 수열에서 한 수를 선택한다. • 그 수보다 작은 수는 그 수 왼쪽으로, 큰 수는 그 수 오른쪽으로 보낸다. • 각 부분에 대하여 이 과정을 반복한다.

  8. 퀵 소트 알고리즘 ( 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; } } while (l < h); t = a[l]; a[l] = a[hi]; a[hi] = t; qsort( a, lo, l-1 ); qsort( a, l+1, hi ); } }

  9. 퀵 소트 알고리즘 ( in Haskell ) qsort [] = [] qsort (x:xs) = qsort lt_x ++ [x] ++ qsort greq_x where lt_x = [y | y<- xs, y< x] greq_x = [y | y<- xs, y>= x]

  10. 비교 분석 • C • 기계적인 과정을 세세하게 처리해 주어야 한다. • 코드를 알아보기가 힘들고, 복잡하다. • 속도가 빠르다. • Haskell • 수학적인 표현을 거의 그대로 옮겼다. • 간결하고, 이해하기 쉽다. • 속도가 C에 비해 느리다.

  11. Haskell의 장점 • 간결하고, 이해하기 쉽다. • 안전하다. (강력한 타입 체크로, 실행 중에 Core dump가 발생하지 않는다.) • Code를 쉽게 재사용할 수 있다. • higher-order function으로, 강력한 추상화가 가능하다. • 메모리 관리를 자동으로 해 준다. • 프로그램의 의미에 혼돈의 여지가 없다.(정확한 정의가 제공된다.)

  12. Haskell의 특징 • 변수가 없다. • Side effect가 없다. (같은 값이 인수로 들어가면 항상 같은 결과가 나온다.) • Loop가 없다. • 프로그램 순서가 없다. J = 1+I I = 5 • 모든 것이 함수이다.

  13. 함수 정의 n :: Int n = 12 + 10 squareInt :: Int -> Int squareInt n = n*n difSquare x y = (x-y)^2 -- polymorphic type f = \x -> 2 * n

  14. Recursion fac1 :: Int -> Int fac1 n = if n==1 then 1 else (n * fac (n-1)) fac2 :: Int -> Int fac2 n = prodList [1 .. n ] prodList lt = if (length h)==1 then head lt else head lt * (prodList (tail lt))

  15. Pattern matching prodLst2 [] = 0 prodLst2 [x] = x prodLst2 (x:xs) = x * prodLst2 xs isSubseq [] _ = True isSubseq _ [] = False isSubseq lt (x:xs) = (lt==start) || isSubseq lt xs where start = take (length lt) (x:xs)

  16. Guards prodLst3 lst | length lst==0 = 0 | length lst==1 = head lst | otherwise = head lst * prodLst3 (tail lst) isSublist [] _ = True isSublist _ [] = False isSublist (e:es) (x:xs) | e==x && isSublist es xs = True | otherwise = isSublist (e:es) xs

  17. List comprehensions myLst :: [(Int,Int,Int)] myLst = [(i,j,i*j) | i <- [2,4..100], j <- [3,6..100], 0==((i+j) `rem` 7)] qsort [] = [] qsort (x:xs) = qsort [y | y<-xs, y<=x] ++ [x] ++ qsort [y | y<-xs, y>x]

  18. Lazy evaluation primes :: [Int] primes = sieve [2 .. ] sieve (x:xs) = x : sieve [y | y <- xs, (y `rem` x)/=0] memberOrd (x:xs) n | x<n = memberOrd xs n | x==n = True | otherwise = False isPrime n = memberOrd primes n

  19. Passing functions qsortF f [] = [] qsortF f (x:xs) = qsortF f [y | y<-xs, f y x] ++ [x] ++ qsortF f [y | y<-xs, not (f y x)] tailComp :: String -> String -> Bool tailComp s t = reverse s < reverse t

  20. Indent rule isMonotonic f n = mapping == qsort mapping where mapping = map f range range = [0..n] iter n f x | n == 0 = x | otherwise = f (iter (n-1) f x)

  21. Reference • http://www.haskell.org • http://ropas.kaist.ac.kr/n • Haskell Tutorials • http://www.di.uminho.pt/afp98/PAPERS/Tutorial.ps • http://www.haskell.org/tutorial/ • Beginning Haskell @ IBM developerWorks

More Related