java, C++에 있는 개념이고 에러 처리를 깔끔하게 할 수 있다는 장점이 있다/
class factRangeException(val arg:Int) extends Exception
def fact(n:Int):Int =
if (n<0) throw new factRangeException(n)
else if (n==0) 1
else n * fact(n-1)
def foo(n:Int) = fact(n+10)
try{
println(fact(3))
println(foo(-100))
}
catch {
case e: factRangeException => {
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
기본 데이터 타입으로는 대표적으로 Int가 있다. Int type 을 예시로 들면
intro for Int : ....,-2,-1,0,1,2,....
elim for Int : +, -, *, / , < , <=, ....
다음 대표적인 type 으로 Tuples 에 대해서 알아보자.
Tuple
intro
(1,2,3):(Int,Int,Int)
(1,'a'):(Int,String)
elim
(1,2,3)._1 = 1
(1,2,3)._2 =.
==> component의 위치로 지정해서 element를 읽어낼 수 있음 ._(위치)
list와는 다르게 인덱스가 0부터 시작하는게 아니라 1부터 시작함
tuple은 데이터타입에 구애받지 않고 만들 수 있고, 길이는 최대 22까지 가능하다
Structural Types (a.k.a. Record Typed)
위에서 살펴본 tuple은 value만 저장이 가능하지만 structure type은 def type도 저장이 가능하다는 특징이 있다. ( block과 큰 연관이 있음 )
Algebraic Data Type
이 개념이 잘 이해가 가질 않아서 위키에 검색해봤다.
In computer programming, especially functional programming and type theory, an algebraic data type (ADT) is a kind of composite type, i.e., a type formed by combining other types.
=> 여러가지 타입의 묶음, 합성 정도로 이해하면 될 것 같다.
Two common classes of algebraic types are product types (i.e., tuples and records) and sum types (i.e., tagged or disjoint unions, coproduct types or variant types).[1]
The values of a product type typically contain several values, called fields. All values of that type have the same combination of field types. The set of all possible values of a product type is the set-theoretic product, i.e., the Cartesian product, of the sets of all possible values of its field types.
The values of a sum type are typically grouped into several classes, called variants. A value of a variant type is usually created with a quasi-functional entity called a constructor. Each variant has its own constructor, which takes a specified number of arguments with specified types. The set of all possible values of a sum type is the set-theoretic sum, i.e., the disjoint union, of the sets of all possible values of its variants. Enumerated types are a special case of sum types in which the constructors take no arguments, as exactly one value is defined for each constructor.
Values of algebraic types are analyzed with pattern matching, which identifies a value by its constructor or field names and extracts the data it contains.
Algebraic data types were introduced in Hope, a small functional programming language developed in the 1970s at the University of Edinburgh.[2]
Prametric Polymorphism
def id[A](x:A):A =.
id(3)
id("abc")
'학교 공부 > 프로그래밍원리' 카테고리의 다른 글
11/10 Type Classes with Multiple Parameters (0) | 2022.11.10 |
---|---|
PART 2 - Object-Oriented Programming (1) (0) | 2022.10.23 |
HW2 - Exercise 2 (0) | 2022.10.19 |
PART 1 - Higher Order Functions (0922) (0) | 2022.10.18 |
PART 1 - Functional Programming with Function Applications (2) (0) | 2022.09.29 |