Question on Figure 6.3 in Thompson's "Craft of Functional Programming"

Submitted by metaperl on Mon, 05/30/2005 - 10:54pm.

In figure 6.3 we see 4 figures. The starting figure is to the "West" and shows two horses superimposed.

My question is, what are the coordinates of the reference point of the two images?

Submitted by Ralph (not verified) on Sat, 06/18/2005 - 12:39pm.

I'm not entirely sure what you refer to with the phrase "reference point" in this case... let's start by looking at the function superimpose:

Recall, a Picture is a list of lists of characters.

type Picture = [[Char]]

Superimpose takes two such pictures of equal size (same number of lists and the same number of elements in each list).

Then it goes through each of the lists and through each of the sub-lists.

superimposeChar :: Char -> Char -> Char
superimposeChar c1 c2 = if c1 == '#' then c1 else c2

superimposeLine :: [Char] -> [Char] -> [Char]
superimposeLine l1 l2 = [superimposeChar c1 c2 | (c1,c2) < - zip l1 l2]

superimpose :: Picture -> Picture -> Picture
superimpose p1 p2 = [superimposeLine l1 l2 | (l1,l2) < - zip p1 p2]

Looking at the code, I would say that the "top-left position" is the "reference point", i.e. the position (0,0):

get_element :: [[Char]] -> (Int, Int) -> Char
get_element p (c1, c2) = p!!c1!!c2

-- Test sample
test = ["123","456","789"]

-- get_element test (0,0) will return 1

HTH,
Ralph

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.