Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
| Comment: | Working Nim example. |
|---|---|
| Downloads: | Tarball | ZIP archive |
| Timelines: | family | ancestors | descendants | both | trunk |
| Files: | files | file ages | folders |
| SHA3-256: |
02c0300a5a945b28824892dc3288b3e6 |
| User & Date: | jaccarmac 2017-12-30 07:30:22.272 |
Context
|
2017-12-30
| ||
| 07:31 | Empty line, trailing whitespace. check-in: b44195af3d user: jaccarmac tags: trunk | |
| 07:30 | Working Nim example. check-in: 02c0300a5a user: jaccarmac tags: trunk | |
| 07:13 | Add Nim example for polymorphic recursion. check-in: 3bb2106de4 user: jaccarmac tags: trunk | |
Changes
Changes to polymorphic-recursion/nim/polymorphic_recursion.nim.
1 2 3 4 5 6 7 8 9 10 |
type
IntListKind = enum
ilNil,
ilCons,
IntList = ref object
case kind: IntListKind
of ilNil: nil
of ilCons:
head: int
tail: IntList
| < < < | < | | | | | | > | | > | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
type
IntListKind = enum
ilNil,
ilCons,
IntList = ref object
case kind: IntListKind
of ilNil: nil
of ilCons:
head: int
tail: IntList
Nested[T] = ref object of RootObj
NestedEpsilon[T] = ref object of Nested[T]
NestedCons[T] = ref object of Nested[T]
head: T
tail: Nested[seq[T]]
proc length(il: IntList): int =
case il.kind
of ilNil: 0
of ilCons: 1 + il.tail.length
method length[T](n: Nested[T]): int {.base.} = quit "override!"
method length[T](n: NestedEpsilon[T]): int = 0
method length[T](n: NestedCons[T]): int = 1 + n.tail.length
let test_il = IntList(kind: ilCons, head: 0, tail: IntList(kind: ilCons, head: 1, tail: IntList(kind: ilNil)))
echo test_il.length
let test_nest = NestedCons[int](head: 0, tail: NestedCons[seq[int]](head: @[1, 2, 3], tail: NestedCons[seq[seq[int]]](head: @[@[1, 2, 3]], tail: NestedEpsilon[seq[seq[seq[int]]]]())))
echo test_nest.length
|