Scheme, schema
Question:
Produce a deep-reverse procedure that takes a list as argument
And returns as its value the list with its elementsReversed
And with all sublists deep-reversed as well.
For example:
(Define x (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:
(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 1 2) (list 3 4 )))
(Fringe x)
(1 2 3 4)
(Fringe (list 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:
(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 giveA 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 ))
Which one should I learn first in common lisp and scheme?
In any development community, I am afraid this question will cause heated debate. To some extent, this is a matter of personal preferences. The following is my opinion.
First, we know that scheme is also a lisp dialect. Therefore, we use lisp to refer to various lisp dialects.
Choose common lisp or scheme. I think it depends on your purpose of learning lisp. eric Raymond said in the article "How To Be a hacker": "Lisp is worth learning. after you master it, you will feel it has inspired you a lot. this will greatly improve your programming level and make you a better programmer ". if you only want to use this purpose, rather than using Lisp as your own language for daily development or even commercial development, you may wish to learn from simpler scheme. in my opinion, scheme is more like a teaching language, MIT's two famous textbooks: Structure and Interpretation of Computer Programs and HTDP (How to Design Programs) all are based on scheme.
If you plan to use lisp as your main development language in the future, you must learn common lisp. This is definitely a powerful language. Apart from the powerful language, there are also rich libraries. graphical interface, and various applications of the server can be used.
Which one should I learn first in common lisp and scheme?
In any development community, I am afraid this question will cause heated debate. To some extent, this is a matter of personal preferences. The following is my opinion.
First, we know that scheme is also a lisp dialect. Therefore, we use lisp to refer to various lisp dialects.
Choose common lisp or scheme. I think it depends on your purpose of learning lisp. eric Raymond said in the article "How To Be a hacker": "Lisp is worth learning. after you master it, you will feel it has inspired you a lot. this will greatly improve your programming level and make you a better programmer ". if you only want to use this purpose, rather than using Lisp as your own language for daily development or even commercial development, you may wish to learn from simpler scheme. in my opinion, scheme is more like a teaching language, MIT's two famous textbooks: Structure and Interpretation of Computer Programs and HTDP (How to Design Programs) all are based on scheme.
If you plan to use lisp as your main development language in the future, you must learn common lisp. This is definitely a powerful language. Apart from the powerful language, there are also rich libraries. graphical interface, and various applications of the server can be used.