duck702
가입: 2011년 3월 3일 올린 글: 1
|
올려짐: 2011년3월11일 5:45 주제: 타입이 ('a -> int -> 'a) -> 'a -> mylist -> 'a 형태로 나오면 어떻게 하나요 |
|
|
# type mylist = Nil | Cons of (int * mylist);;
type mylist = Nil | Cons of (int * mylist)
# let rec fold_left f init lst = match lst with Nil -> init | Cons(head, tail) -> fold_left f (f init head) tail;;
val fold_left : ('a -> int -> 'a) -> 'a -> mylist -> 'a = <fun>
# let _ = print_int (fold_left (fun x y -> x + y) 0 Cons(1, Cons(2, Cons(3, Nil))));;
Characters 41-42:
let _ = print_int (fold_left (fun x y -> x + y) 0 Cons(1, Cons(2, Cons(3, Nil))));;
^
Error: This expression has type 'a -> 'b but is here used with type int
# let _ = print_int (fold_left (fun x y -> x + y) 0 (Cons(1, Cons(2, Cons(3, Nil)))));;
6- : unit = ()
Exercise 1.3을 이렇게 짜려고 했는데 형식이 'a 꼴로지정되어버리고 실행도 Cons앞에 괄호를 붙였을때면 올바르게 나옵니다. 왜 그렇고 어떻게 해야하나요 |
|