开发者

Scheme, lists, and graph theory

开发者 https://www.devze.com 2023-04-10 04:57 出处:网络
I\'m trying to develop a Scheme function that will take a graph as defined: (define aGraph \'{{USA {Canada Mexico}}

I'm trying to develop a Scheme function that will take a graph as defined:

(define aGraph
     '{{USA {Canada Mexico}}
       {Mexico {USA}}
       {Canada {USA}}})

and find the number of nodes bordering the specified node of the graph. I believe I am approaching this problem the wrong way; here is what I have done so far:

(define (nodes n graph)
      (cond ((null? n) '())
      (else
      (cond ((eqv? n (first graph)) (length (first graph)))
      (else (nodes n (rest graph)))))))

Needless to say, it doesn't work (The function can be called like this: (nodes 'USA aGraph), which in theory should return 2). What advice d开发者_JAVA百科o you have to offer so that I may get on the right track?


Let's examine this line:

  (cond ((eqv? n (first graph)) (length (first graph)))

You are treating (first graph) as both the node key in (eqv? n (first graph)) and as the bordering nodes in (length (first graph)) -- perhaps this will work better:

  (cond ((eqv? n (first (first graph))) (length (second (first graph))))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号