[Scheme] Verify palindromic

[Scheme] Verify palindromic


topic

Write a recursive function to determine if the elements of a list form a palindrome(回文: 从左读与从右读都一样). For example:
palindrome : List -> Bool
> (palindrome ‘(1 1 2 2 3 3))
#f
> (palindrome ‘(1 2 2 2 1))
#t
> (palindrome ‘(1 a b a 1))
#t
Note : For simplicity, you may assume that the list to check contains only atoms. I.e., no nested list and you can use “eq?” to test for equality


The problem simply,
the argument is a list, our function will return this list whether they have the characteristics of palindromes.

The idea
idea most people should start with me, like, what to write this recursive? Not reverse his party to get thing?

	(define (palindrome-reverse a-list)
  (equal? a-list (reverse a-list)))

ya ... Yes, indeed this line to meet our needs.

But mention the topic, please write a recursive function to do,
the class also repeatedly mentioned, high-order function of these built-in function, but also by one a smaller scheme function composition.

So, write your own hands it ~ reverse function

design ideas recursive function

  1. Judgment palindrome, which is equal to its own list upside down, return true, does not mean the return false.
  2. recursive design ideas, how upside down:
    Like to meet tail-recursion, we will start with a accumulating parameter, to put every scratch results.
    Each taking (car list), then append to pay attention to the order, the result is to be added to the front of, or behind.
    Since we want to do backwards, that is to (append (the car list of element) result)
  3. When cdr list is empty, it means that all take over, return our results


So I wrote code a long way:

	;case 1: (palindrome '(1 1 2 2 3 3)) => #f
;case 2: (palindrome '(1 2 2 2 1)) => #t
;case 3: (palindrome '(1 a b a 1)) => #t
(define (tailReverse a-list result)
  (if (null? a-list) 
      result
      (tailReverse (cdr a-list) (append (list (car a-list)) result)))
)

(define (palindrome a-list)
  (equal? a-list (tailReverse a-list '()))    
)


Perhaps you would be interested in the following training courses:

  1. 2019/7/27 (six) ~ 2019/7/28 (day): Evolution of design: Test-driven development and continuous refactoring sixth echelon (Chinese Taipei)
  2. 2019/8/16 (e) ~ 2019/8/18 (day): [C # Advanced Design - Design learn from the reconstructed high ease of use and high flexibility API] second echelon (Chinese Taipei)
  3. 2019/9/21 (six) ~ 2019/9/22 (day): Clean Coder: DI and AOP advanced combat second echelon (Chinese Taipei)
  4. 2019/10/19 (f): [Art added unit tests for legacy code] seventh echelon (Chinese Taipei)
  5. 2019/10/20 (day): [development] Speed ​​eighth echelon (Chinese Taipei)

Would like to receive first-hand information public training courses, or would like to inquire about house training, consulting, coaching, consulting services, please contact Facebook fan page: 91 agile development of the road.

Original: Large column  [Scheme] verification palindrome


Guess you like

Origin www.cnblogs.com/petewell/p/11445518.html