junkcode  Check-in [02c0300a5a]

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: 02c0300a5a945b28824892dc3288b3e6ac93a4a057f4cb2dbfaccb03a6a6b71c
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
Unified Diff Ignore Whitespace Patch
Changes to polymorphic-recursion/nim/polymorphic_recursion.nim.
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
34
35
type
  IntListKind = enum
    ilNil,
    ilCons,
  IntList = ref object
    case kind: IntListKind
    of ilNil: nil
    of ilCons:
      head: int
      tail: IntList
  NestedKind = enum
    nEpsilon,
    nCons,
  Nested[T] = ref object
    case kind: NestedKind
    of nEpsilon: nil
    of nCons:
      head: T
      tail: Nested[seq[T]]

proc length(il: IntList): int =
  case il.kind
  of ilNil: 0
  of ilCons: 1 + il.tail.length

proc length(n: Nested): int =
  case n.kind

  of nEpsilon: 0
  of nCons: 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 = Nested[int](kind: nCons, head: 0, tail: Nested[seq[int]](kind: nCons, head: @[1, 2, 3], tail: Nested[seq[seq[int]]](kind: nEpsilon)))
echo test_nest.length










<
<
<
|
<
|
|
|
|






|
|
>
|
|
>




|

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