lisp - How do I write Push and Pop in Scheme? -


I have now

  (define x a-list) (set! List (list one list) (set a list x)) (defined (pop one list) (let (result (first one list))) (set one list (rest one list)))  

The result is:

  Welcome to DosKemeeme, version 4.2 [3M]. Language: module; Memory limit: 256 megabytes & Gt; (Define my list (list 1 2 3)) & gt; (Press my list 4) & gt; My List (1 2 3) & gt; (Pop-marie-list) 1 & gt; My list (1 2 3)  

What am I doing wrong? Is Push a better way of writing so that the elements are finally added and popped so that the element is already removed?

This is a point about using mutation in your code: for it to jump into macros There is no requirement. I'll consider the stack operation for now: to get a simple price that you can mutate around, you only have one cover around the list and the rest of the code remains the same (well, small changes Along with it works properly). In PLT scheme this is exactly what is for the box:

  (define x one list) (set-box! A-list (opposition x (unbox a-list)))) (Set-box! A-list (rest (unbox one-list)) results))  <() / pre> 

Note that you let0 :

begin0 instead of
  (pop up a list) (Start0 (first (unboxed a-list)) (set-box! A-list ( The rest (unbox one list)))  

To convert it to a queue, You can use one of the following methods, but except for the last version written by Jonas, solutions are very inadequate, for example, if you have suggested Save:

  (set- Box! Queue (append (unbox queue) (list x)))  

then it has copies of the entire queue - which means adding items to your queue A loop in which will copy all on each joint, will produce many trash for GC (end) Think of adding a character in a string in a loop) "unknown" (Google) modifies the list of solutions and adds an indicator at the end, so it avoids causing garbage to be collected. , But it's still disabled.

The solution written by Jonas is that simple way - it keeps an indicator at the end of the list. However, if you want to do it in PLT scheme, you will need to use the variable joints: mcons , mcar , mcdr , Set-Makers! , Set-MCDR! . Since version 4.0, common pairs in PLT are irreversible.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -