Some Understanding of Scheme (3)

Source: Internet
Author: User

I read chapter 3 on and off, and I think scheme is a little getting started. However, if I don't practice for a long time, my mind can't adapt to functional programming. I think 3.17, 3.18, and 3.19 are quite interesting, post your own solutions.

3.17

(define (count-unique-pair X)
(LET (dB (cons 0 0)
(define (count-pair X)
(define (add-unique-pair x db1)
(define (add-record db0 X)
(set-car! Db0 X)
(set-CDR! Db0 (cons 0 0)
X)
(define (find-record x db0)
(if (not (pair? (Car db0)
(add-record db0 X)
(if (EQ? X (CAR db0)
'()
(find-record X (CDR db0)
(find-record x db1 ))
(if (not (pair? X)
0
(if (null? (Add-unique-pair x dB)
0
(+ (count-pair (car x)
(count-pair (cdr x ))
1)
(count-pair X)

(Define XX '(a B ))
(Define Z1 (cons XX ))
(Define Z2 (cons '(a B )))
(Count-unique-pair Z1)

The implementation is ugly, basically procedural programming. We use a dB to store pair that has already been traversed. We can deal with a table with loops, and as long as the pair has already been traversed, the sub-pair is not traversed. Compared with pure functional writing, this method is clear and easy to understand, but there is a memory competition problem and it cannot be run in parallel.

3.18

This question is simplified. You don't need to consider the car ring. It's okay to use the 3.17 storage method.

3.19

This requires a small trick and a space constant, which means that the pair pointer that has already been traversed cannot be stored. So I will change the space by time and use two pointers. The a pointer will step one pair at a time, the B pointer Step 2 pair at a time. If there is a ring, the B pointer will catch up with the pointer again. You can check whether the pointer is equal at each step to see if it has a ring :)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.