목록Tutorials (19)
KoreanFoodie's Study

These are OCaml Library module "List" implementation. Useful when we handle list data type in OCaml. let rec length l = match l with | [] -> 0 | _::t -> 1 + length t (* tail-recursive version of length *) let length' l = let rec f l result = match l with | [] -> result | _::t -> f t (result+1) in f l 0 let hd l = match l with | [] -> raise (Failure "hd") | h::_ -> h let tl l = match l with | [] ..

예제들을 OCaml로 구현해 보자. OCaml quicksort code (* pick the first element as a pivot *) (* ascending order *) let rec qsort comp l = match l with | [] -> [] | p::t -> let (l1,l2) = List.partition (fun x -> comp x p print_int x;print_string " ") l; print_newline(); print_endline "result of qsort : "; List.iter (fun x -> print_int x;print_string " ") (qsort compare l); print_newline() OCaml Stack code 모듈..

이전 게시글에 이어 OCaml에서 꼭 알아야 할 중요한 개념들에 대해 더 알아보도록 하자. 주요 개념들 : Pair, Tuple, List, Currying, Inductive type, match-with 구문, Polymorphic type, try-with-raise, module system, reference. OCaml Pair Pair는 두 개의 값을 한번에 묶는 데 활용된다. 정의는 (x, y)같은 식으로 할 수 있으며, 타입은 a * b형태로 표기된다. Pair의 각 항을 조회하는데 fst와 snd함수를 사용할 수 있다. let p:(int * string) = (1, "a") let i = fst p (* int, 1 *) let s = snd p (* string, "a" *) let..

OCaml 언어 : Ocaml은 여러가지 사용법을 가지는 상위 (high-level)언어이다. 절차형(imperative), 객체지향(object oriented), 함수형(functional) 등의 프로그래밍을 모두 지원한다. Java처럼, 자동으로 메모리를 관리해 주는 Garbage collector가 존재한다. 타입 시스템이 내장되어 매우 안정적으로 사용할 수 있다. 절차형 VS 값 중심형 절차형 언어는 기게에게 순서대로 명령을 전달한다. 함수형 언어의 경우, 값 중심으로 기계에게 식의 계산을 시키는 식으로 동작한다. OCaml 기초 - 값 정의 정수 값 let i = 1 문자열 값 let s = "hello world" Boolean 값 let b = true unit let _ = print_e..

스칼라 튜토리얼, scala의 Abstract class에 대해 알아보자 Abstract Class: Specification Abstract Classes can be used to abstract away the implementation details. In short, abstract classes is used for specfication, and concrete sub-classes for implementation. Example: Abstract case for specification abstract class Iter[A] { def getValue: Option[A] def getNext: Iter[A] } def sumElements[A](f: A=>Int)(xs: Iter[A])..

스칼라 튜토리얼, scala의 class에 대해 알아보자 Class: Parameterized Record object gee { val a : Int = 10 def b : Int = a + 20 def f(z: Int) : Int = b + 20 + z } type gee_type = {val a:Int; def b: Int; def f(z:Int): Int} class foo_type(x: Int, y: Int) { val a : Int = x def b : Int = a + y def f(z: Int) : Int = b + y + z } val foo : foo_type = new foo_type(10,20) If you define class, new type is made. The name r..

스칼라 튜토리얼, scala의 sub types에 대해 알아보자 Sub Type Polymorphism (Concept) Let's assume that we want this: object tom { val name = "Tom" val home = "02-880-1234" } object bob { val name = "Bob" val home = "02-123-1234" } def greeting(r: ???) = "Hi " + r.name + ", How are you?" greeting(tom) greeting(bob) // Note that we have tom: {val name: String; val home: String} bob: {val name: String; val mobile: ..

스칼라 튜토리얼, scala의 parametric polymophism에 대해 알아보자 Parametric Polymorphism: Functions Problem def id1(x: Int): Int = x def id2(x: Double): Double = x Can we avoid DRY(Do not Reapeat Yourself)? Parametric Polymorphism(a.k.a. For-all Types) def id[A](x: A): A = x // type for definition val f = (x:Int) => x // type for value The type of id is [A](x: A)A. id is a parametric expression. Only definition..

스칼라 튜토리얼, scala datatypes part2 Pattern Matching A way to use algebraic datatypes Syntax: e match { case C1(...) => e1 ... case Cn(...) => en } Example : sealed abstract class IList case class INil() extends IList case class ICons(hd: Int, tl: IList) extends IList def x: IList = ICons(2, ICons(1, INil())) def length(xs: IList) : Int = xs match { case INil() => 0 case ICons(x, tl) => 1 + length(t..

스칼라 튜토리얼, scala의 datatypes에 대해 알아보자 Types in general Introdection : How to construct elements Elimination : How to use elements of the type Primitive types : Int, Boolean, Double, String... Intro for Int: ...,-2,-1,0,1,2 Elim for Int: +,-,*,/... -Function types : Int => Int, (Int => Int)=>(Int => Int) Intro: (x:T)=>e Elim: f(v) Tuples Intro : (1, 2, 3) : (Int, Int, Int) (1, "a") : (Int, String) ..