In the first implementation of grandparents-of function in sec. 7.1.1.2,
The Python-style type annotations
fun grandparents-of(anc-table: Table, person: String) -> List[String]:
should be changed to
fun grandparents-of(anc-table :: Table, person :: String) -> List<String>:
In the three test cases
grandparents("Anna") is [list: "Laura", "John"]
grandparents("Laura") is [list:]
grandparents("Kathi") is [list:]
All three use wrong function name and lack the first argument.
The corrected left side of first test grandparents-of(ancestors, "Anna") returns [list: "Ellen", "Bill"], not [list: "Laura", "John"]. Even grandparents-of(ancestors, "Susan") won't return [list: "Laura", "John"] because the male-parent of Susan is not found.
The corrected left side of last test grandparents-of(ancestors, "Kathi") raises an error "No such person", rather than returning an empty list.
In the second implementation of graendparents-of function in the same section,
The second function argument is called name, while it's called person in the first implementation.
Two uses of plist.length in conditionals should be plist.length(). plist.length == 2 always return false because a function is never equal to a number.
To test the case where plist.length() == 1 is true, I suggest to add a test case
grandparents-of(ancestors, "John") is [list: ]
In total,
diff --git a/old.arr b/new.arr
index aa866fd..c598594 100644
--- a/old.arr
+++ b/new.arr
@@ -1,22 +1,23 @@
-fun grandparents-of(anc-table: Table, person: String) -> List[String]:
+fun grandparents-of(anc-table :: Table, person :: String) -> List<String>:
doc: "compute list of known grandparents in the table"
# glue together lists of mother's parents and father's parents
plist = parents-of(anc-table, person) # gives a list of two names
parents-of(anc-table, plist.first) +
parents-of(anc-table, plist.rest.first)
where:
- grandparents("Anna") is [list: "Laura", "John"]
- grandparents("Laura") is [list:]
- grandparents("Kathi") is [list:]
+ grandparents-of(ancestors, "Anna") is [list: "Ellen", "Bill"]
+ grandparents-of(ancestors, "John") is [list: ]
+ grandparents-of(ancestors, "Laura") is [list: ]
+ grandparents-of(ancestors, "Kathi") raises "No such person"
end
-fun grandparents-of(anc-table :: Table, name :: String) -> List<String>:
+fun grandparents-of(anc-table :: Table, person :: String) -> List<String>:
doc: "compute list of known grandparents in the table"
# glue together lists of mother's parents and father's parents
- plist = parents-of(anc-table, name) # gives a list of two names
- if plist.length == 2:
+ plist = parents-of(anc-table, person) # gives a list of two names
+ if plist.length() == 2:
parents-of(anc-table, plist.first) + parents-of(anc-table, plist.rest.first)
- else if plist.length == 1:
+ else if plist.length() == 1:
parents-of(anc-table, plist.first)
else: empty
end
Contact Details
No response
Which Web page has the problem?
https://dcic-world.org/2023-02-21/trees.html#%28part._.Computing_.Grandparents_from_an_.Ancestry_.Table%29
What's the problem?
In the first implementation of
grandparents-of
function in sec. 7.1.1.2,should be changed to
grandparents-of(ancestors, "Anna")
returns[list: "Ellen", "Bill"]
, not[list: "Laura", "John"]
. Evengrandparents-of(ancestors, "Susan")
won't return[list: "Laura", "John"]
because the male-parent of Susan is not found.grandparents-of(ancestors, "Kathi")
raises an error "No such person", rather than returning an empty list.In the second implementation of
graendparents-of
function in the same section,name
, while it's calledperson
in the first implementation.plist.length
in conditionals should beplist.length()
.plist.length == 2
always return false because a function is never equal to a number.plist.length() == 1
is true, I suggest to add a test caseIn total,
What browser are you seeing the problem on?
Chrome