본문 바로가기

학교 공부/프로그래밍원리

11/16 Type Classes With Multiple Parameters & Higher-kind http://jaynewho.com/post/3 http://jaynewho.com/post/4 testList[List[Int]] implicit def listIter[A] : Iter[List[A], A] = new Iter[List[A],A] { def getValue(a: List[A]) = a.headOption def getNext(a: List[A]) = a.tail } implicit def listIF[A] : ListIF[List[A],A] = new ListIF[List[A],A] { def empty: List[A] = Nil def head(l: List[A]) = l.headOption def tail(l: List[A]) = l.tail def cons(a: A, l: Lis.. 더보기
11/10 Type Classes with Multiple Parameters trait Ord[A] { def cmp(me: A, you: A): Int //Data가 없기 때문에 this가 없음. def ===(me: A, you: A): Boolean = cmp(me,you) == 0 def (me: A, you: A): Boolean = cmp(me,you) > 0 def = 0 } def max3[A](a: A, b: A, c: A)(implicit ord: Ord[A]) : A = if (ord. case Some(n) => {println(n); printElements(IT.getNext(xs))}} def sumElements2[R](xs: R)(implicit ITR: It.. 더보기
PART 2 - Object-Oriented Programming (1) Sub Type Polymorphism A가 B의 subtype이면 A가 B보다 더 구체적인 것. (B를 그대로 상속 받고 --> 다음 A가 자체적으로 더 구체화 한게 있다는 의미로 받아드리면 됨) 따라서 B에 대해서 정의된 모든 함수에 대해서 A도 사용해도 된다. (B에 정의된 것은 모두 A에 기본적으로 정의되어 있기 때문이다.) simplification using Argument Members class MyList[A](v:A, nxt:Option[MyList[A]]){ val value:A = v val next:Option[MyList[A]] = nxt } class MyList[A](val value:A, val next:Option[MyList[A]]){ } type T1 = {val a:.. 더보기
PART 1 - Exception & Handling , DataType (0929) java, C++에 있는 개념이고 에러 처리를 깔끔하게 할 수 있다는 장점이 있다/ class factRangeException(val arg:Int) extends Exception def fact(n:Int):Int = if (n { println("fact range error: " + e.arg) } } 이 코드를 실행하면 출력 값으로 6 fact range error: -90 이 나온다. DataType 에 대해서 알아보자 Types는 introduction operations과 eloimination이 필요하다. Introduction : how to construct elements of the type Elimination : how to use elements of the type 기본 .. 더보기
HW2 - Exercise 2 /** * Problem 2: Reversed Binary Representation (5 points each) * * Implement the basic operations of reversed binary representation (RBB). * You should use the predefined `BNum` type from Data.scala file. * * RBB is one way to represent natural number through ADT. * * BHead: Most significant bit (1) * B1: One (1) * B0: Zero (0) * * e.g.) * number 1 -> BHead * number 6 -> /binarize/ 110 (2) -> /.. 더보기
PART 1 - Higher Order Functions (0922) type을 사용하려면 1) value 표시 (introduction) 2) 연산 (elimination) : type 값으로 뭘 할 수 있는지 Higher Order Functions은 코드의 재사용성과 가독성을 높임 Higher Order Function의 예시는 def sum(f:Int=>Int, n:Int):Int = if (nx,n) def sumSquare(n:Int) = sum((x:Int)=>x*x,n) def sumCube(n:Int) = sum((x:Int)=>x*x*x,n) 이거는 소름돋는 예제 다음은 closure에 대해서 배웠다. 교수님 피셜 closure은 코드와 그 코드가 정의됐을 때의 환경을 페어링 해주는 도구라고 하셨다. 일종의 여권? 느낌!! closure가 사용된 예시 코.. 더보기
PART 1 - Functional Programming with Function Applications (2) 이 포스팅은 3차시 내용에 관한 것이다 이제부터 살짝 헷갈릴 예정 복습을 많이 해야하는 부분이다 Evaluation Strategy : Call-by-name , Call-by-value ▶ Call-by-value (cbv) value로 call을 한다는 의미이니 value로 먼저 계산을 다 하고 나서 치환된다. 정확한 설명은 Evaluete the arguments first, then apply the function to them. 이라고 되어있다. ▶ call-by-name (cbn) 위에서 콜바이밸류는 이름값을 한다. value로 소환하니까.. 근데 이건 교수님이 왜 name인지 모르겠다고 하셨다 call-by-expression이 더 직관적으로 와닿는다. 이건 함수 body에 그냥 expre.. 더보기
PART 1 - Functional Programming with Function Applications (1) 오늘은 Values, Names, Functions and Evaluations에 대해서 포스팅 할 것이다. ▶ Types and Values Type is a set of values Int : {-2147483648, ..., -1, 0, 1, ..., 2147483647} // 32-bit integers Double : 64-bit floating point numbers Boolean : {true, false} ... 여기서 Java랑 비슷하게 Int 타입 범위에는 제한이 있다는 걸 기억해 두는 것이 좋다. 대충 2 x 10^9 로 암기해두자. ▶ Expression 교수님이 한 수업 당 최소 50번 언급하시는 그 단어,,, 익스프레션,,,, Expression은 values, names, p.. 더보기