Google Answers Logo
View Question
 
Q: SOLUTION ( No Answer,   3 Comments )
Question  
Subject: SOLUTION
Category: Computers > Programming
Asked by: mott85-ga
List Price: $33.00
Posted: 26 Oct 2004 21:30 PDT
Expires: 04 Nov 2004 19:17 PST
Question ID: 420594
This is a typical thing to do in scheme can any find soultion?

Write a procedure length that takes an arbitrary list structure and
returns its length if it is a list without loop back. If there is a
loop back, clength returns a list of two numbers (m n) where m is the
number of pairs in the initial segment of the input and n is the
number of pairs in the circle caused by the loop back.

Clarification of Question by mott85-ga on 31 Oct 2004 22:47 PST
The code i am looking for is not for a homework problem i had recived
this excecse through a party that is tring to help me leran scheme i
am unable to contavt them for helpa nd i am lookign to see if soemone
else can help provide that
Answer  
There is no answer at this time.

Comments  
Subject: Re: SOLUTION
From: obten-ga on 28 Oct 2004 21:04 PDT
 
O, those are not the tipical things to do in Scheme! 
(why? read after the answer of your question)

Scheme is an elegant descentant of LISP, the first functional programming language.
LISt Processing, was the first programming language defining it's
semantics axiomatically.

The main data structure is the list. 

There are three kinds of lists in lisp (and scheme). 

Proper lists, dotted lists, and circular lists.

Proper lists are inductively defined as follows:

  i) nil is a (proper) list
 ii) (x . xs) is a list  if xs is a (proper) list 
iii) only the objects formed by rules i-ii are (proper) lists.

(x . xs) is other notation for (cons x xs) the constructor. 
 
(a b c d) is a brief form of (a . (b . (c . (d . nil))))

In statically-typed languages a proper list may be defined as follows:

  i) nil is a list of type 'a
 ii) (x . xs) is a list  if xs is a list of type 'a and x is of type 'a
iii) only the objects formed by rules i-ii are lists of type 'a.

(a b c) is a legal list of symbols
((1) (2) (3)) is a legal list of lists of numbers
(a (a) 2) is an illegal list in a statically typed language (like SML or Haskell)

Dotted lists are a more general data structrure, because lisp is
dynamically typed language,
it is possible to define lists of objects of any type not homogeneous,
like the second definition of a list of type a.

A dotted list is defined as follows

  i) x is an atom then it is a (dotted) list
 ii) (x . xs) is a list  if xs is a (dotted) list
iii) only the objects formed by rules i-ii are (dotted) lists.

As you can see, the only difference between proper lists and dotted
list (in lisp or scheme)
is that they can end with an atom different of nil. a number or symbol.

Actually dotted lists are binary trees  for exampe  '((a . b) . (c .
d)) in short notation ((a . b) c . d)
is a representation of a binary tree with leafs a,..,d

Circular lists are constructed with set-cdr! in order to allow what
you call "loop back",
in that case the "last" element of a "list" is a pointer to some pair
in the "list".

   ( a . ( b .	| ))
     ^          |	
     +-------+

How could you find when some element is repeating, 

first you must remember that lisp's EQ compairs pointers while EQUAL
when faced with two
lists compairs each element recursively. 
In Scheme you have eq? and eqv?.

A way to find the link to the repeating section of the circular list
is to build a list of lists to the
tails recursively, use it as a parameter and testing every time for
membership of the cdr,
if it is included you got the place where the pointer is.

It is not efficient, but works.   I do not write the code, because
your question seems part of a homework.

In order to improve performance, it is not wise to do such waste of
time, it is better to build an abstract data type
where the pointer held by an encapsulated variable. 

See the wizards book (Abelson, Susmann, Structure and Interpretation
of Computer Programs, MIT Press)
to find more about data absraction and encapsulation.

The use of destructive "functions" like set-car!, set-cdr! must
restricted to very exceptional cases, use carfuly.

The style of Scheme is more functional than imperative, don't use
imperative (destructive) "functions" if it is note strictly needed.

Function application and composition is more typical.  
Scheme is an eager language, but you can get some lazyness with the
use of streams, in order, for example to
implement an infinite list of ones, or all the natural numbers (and
you do not need to make use of circular lists).
 
You can also check the following reports to know more about scheme (and lisp):

Revised report on the algorithmic language Scheme.
R. Kelsey, W. Clinger, J. Rees (editors). 
Higher-Order and Symbolic Computation, Vol. 11, No. 1, September, 1998. 
and ACM SIGPLAN Notices, Vol. 33, No. 9, October, 1998. 

Common Lisp: the Language
Guy L. Steele Jr. (editor).
Digital Press, Maynard, Mass., second edition 1990.
Subject: Re: SOLUTION
From: mott85-ga on 30 Oct 2004 22:04 PDT
 
That kind of helps Thanks. I ams still alittle confused ont he code i
would really like to see thsi work if nayone would like to help with
that i owuld be most apprecative.
Subject: Re: SOLUTION
From: kevko-ga on 04 Nov 2004 11:45 PST
 
Sorry if the code looks funny.  My lisp is rusty (I'm an ML guy), and
I'm applying those old memories to this dialect.  This is a naive
solution; it's got a quadratic factor due to the searching of
previously seen entries.

Oops.  Seems like only 'answer' guys get payment.   Oh well for me--here you go:

(define length-circ (lambda (lst)
		      (letrec 
			  ((cntkey (lambda (l k cnt)
				   (if (eqv? l k) cnt 
				       (cntkey (cdr l) k (+ cnt 1)))))
			   (crec 
			    (lambda (l seen tcnt)
			      (cond ((null? l) (list tcnt 0))
				    ((find-matching-item 
				      seen 
				      (lambda (e) (eqv? (car e) l)))
				     (let ((icnt (cntkey lst l 0)))
				       (list icnt (- tcnt icnt))))
				    (else (crec (cdr l) 
						(cons (list l) seen)
						(+ tcnt 1)))))))
			   (crec lst () 0))))

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy