Scheme,schema

來源:互聯網
上載者:User

Scheme,schema


Question:

produce a deep-reverse procedure that takes a list as argument 

and returns as its value the list with its elements reversed 

and with all sublists deep-reversed as well.


For example:

(define x (list (list 1 2) (list 3 4)))

x

((1 2) (3 4))

(reverse x)

((3 4) (1 2))

(deep-reverse x)

((4 3) (2 1))


Code:

( define tree ( list 1 ( list 2 ( list 3 4 ) 5 ) ( list 6 7 ) ) )
( define nil '() )


( define ( my-reverse items )
   ( define ( rev-imp items result )
      ( if ( null? items ) 
           result 
           ( rev-imp ( cdr items )
                     ( cons ( car items ) result ) ) ) )
   ( rev-imp items nil ) )
( my-reverse items )


Output:

> ( my-reverse tree )
((6 7) (2 (3 4) 5) 1)


Code:

( define ( deep-reverse items )
   ( define ( deep-rev-if-required item )
      ( if ( not ( pair? item ) ) 
           item 
           ( deep-reverse item ) ) )
   ( define ( deep-rev-imp items result )
      ( if ( null? items ) 
           result 
           ( deep-rev-imp ( cdr items )
                          ( cons ( deep-rev-if-required( car items ) ) 
                                 result ) ) ) )
   ( deep-rev-imp items nil ) ) 


Output:

> ( deep-reverse tree )
((7 6) (5 (4 3) 2) 1)


or Code as:

( define ( deep-reverse items )
   ( if ( pair? items )
        ( my-reverse ( map deep-reverse items ) )
        items ) )


Output:

> ( deep-reverse tree )
((7 6) (5 (4 3) 2) 1)




Question:

Write a procedure fringe that takes as argument a tree  (represented as a list) and 

returns a list whose elements are all the leaves of the tree arranged in left-to-right order. 


For example:

(define x (list (list 1 2) (list 3 4)))

(fringe x)

(1 2 3 4)

(fringe (list x x))

(1 2 3 4 1 2 3 4)


Code:

( define ( fringe tree )
   ( define ( search items res )
      ( cond ( ( null? items ) 
               res )
             ( ( not ( pair? items ) ) 
               ( cons items res ) )
             ( else ( search ( car items )
                             ( search ( cdr items ) res ) ) ) ) )
   ( search tree nil ) )


or Code as:

( define ( fringe tree )
   ( cond ( ( null? tree ) 
            nil )
          ( ( not ( pair? tree ) )
                ( list tree ) )
          ( else ( append ( fringe ( car tree ) )
                          ( fringe ( cdr tree ) ) ) ) ) )


Output:

> ( fringe tree )  
(1 2 3 4 5 6 7)




Question:

We can represent a set as a list of distinct elements, 

and we can represent the set of all subsets of the set as a list of lists. 

For example, if the set is (1 2 3), then the set of all subsets is 

(() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)). 

Complete the following definition of a procedure that

generates the set of subsets of a set and give a clear explanation of why it works:


For example:

( define ( subsets s )
  ( if ( null? s )
      ( list nil )
      ( let ( ( rest ( subsets ( cdr s ) ) ) )
        ( append rest ( map <??> rest ) ) ) ) )


Code:

( define nil '() )
( define ( subsets s )
   ( if ( null? s )
        ( list nil )
        ( let ( ( rest ( subsets ( cdr s ) ) ) )
           ( append rest ( map ( lambda ( x )
                                ( cons ( car s ) x ) )
                               rest ) ) ) ) )


Output:
> ( subsets ( list 1 2 3 ) )

(() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3))


common lisp與scheme,應該先學哪個?

在任何一個開發社區提問這個問題, 恐怕都會引起激烈的爭論. 一定程度來說, 這是一個 個人偏好問題. 下面是我的看法.

首先, 我們知道 scheme 也是一種 lisp 方言, 因此下文用 lisp 來泛指各種 lisp 方言.

選擇 common lisp 還是 scheme, 我認為這要看你學習 lisp 的目的. Eric Raymond 在 "如何成為一名駭客" 一文中說過: "Lisp 很值得學習. 你掌握它以後, 會感到它給你帶來的極大的啟發. 這會大大提高你的編程水平, 使你成為一個更好的程式員 ". 如果你僅僅以此為目的, 而不打算把 Lisp 作為自己日常開發甚至商業開發的語言, 不妨從更簡潔一些的 scheme 學起. 在我看來, scheme 更像是一種教學型語言, MIT 的兩本著名教材 SICP (Structure and Interpretation of Computer Programs) 和 HTDP (How to Design Programs) 都是以 scheme 為基礎的.

如果你打打算把 lisp 作為自己今後的主要開發語言, 那麼就一定要學學 common lisp 了, 這絕對是一個強大的語言, 除了語言本身的強大, 還有豐富的庫. 圖形介面, 伺服器什麼的各種應用都可以做.
 
common lisp與scheme,應該先學哪個?

在任何一個開發社區提問這個問題, 恐怕都會引起激烈的爭論. 一定程度來說, 這是一個 個人偏好問題. 下面是我的看法.

首先, 我們知道 scheme 也是一種 lisp 方言, 因此下文用 lisp 來泛指各種 lisp 方言.

選擇 common lisp 還是 scheme, 我認為這要看你學習 lisp 的目的. Eric Raymond 在 "如何成為一名駭客" 一文中說過: "Lisp 很值得學習. 你掌握它以後, 會感到它給你帶來的極大的啟發. 這會大大提高你的編程水平, 使你成為一個更好的程式員 ". 如果你僅僅以此為目的, 而不打算把 Lisp 作為自己日常開發甚至商業開發的語言, 不妨從更簡潔一些的 scheme 學起. 在我看來, scheme 更像是一種教學型語言, MIT 的兩本著名教材 SICP (Structure and Interpretation of Computer Programs) 和 HTDP (How to Design Programs) 都是以 scheme 為基礎的.

如果你打打算把 lisp 作為自己今後的主要開發語言, 那麼就一定要學學 common lisp 了, 這絕對是一個強大的語言, 除了語言本身的強大, 還有豐富的庫. 圖形介面, 伺服器什麼的各種應用都可以做.
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.