Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming > #6200
| From | BartC <bc@freeuk.com> |
|---|---|
| Newsgroups | comp.programming |
| Subject | Re: Rotation |
| Date | 2016-01-05 12:22 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <n6gcdv$vus$1@dont-email.me> (permalink) |
| References | <f8f1edff-9175-4210-8877-fdd0ffa4f42c@googlegroups.com> <n6f5qs$lme$1@dont-email.me> <20160104205905.491@kylheku.com> |
On 05/01/2016 04:59, Kaz Kylheku wrote:
> On 2016-01-05, BartC <bc@freeuk.com> wrote:
>> On 04/01/2016 17:25, Paul wrote:
>>> I can verify that this code works for special cases but it's not
>>> intuitively clear that it should always work.
>> proc rotate(&a,middle)=
>> first := a.lwb
>> last := a.upb+1
>> next := middle
>>
>> while first <> next do
>> swap(a[first++], a[next++])
>> if next = last then
>> next := middle
>> elsif first = middle then
>> middle := next
>> fi
>> od
>> end
>
> Lol, blub languages.
>
> $ ./txr
> This is the TXR Lisp interactive listener of TXR 129.
> Use the :quit command or type Ctrl-D on empty line to exit.
> 1> (defparm a (range 0 9))
> a
> 2> (defparm b (range 0 9))
> b
> 3> a
> (0 1 2 3 4 5 6 7 8 9)
> 4> b
> (0 1 2 3 4 5 6 7 8 9)
> 5> (rotate [a 0..8] [a 8..:])
> (0 1 2 3 4 5 6 7)
> 6> (rotate [b 0..2] [b 2..:])
> (0 1)
> 7> a
> (8 9 0 1 2 3 4 5 6 7)
> 8> b
> (2 3 4 5 6 7 8 9 0 1)
How does this relate to the problem of verifying the original C++ code?
Is it any easier to understand? It seems you are not actually
implementing a 'rotate' function function at all (AFAICS), but simply
using some built-on operator.
Then of course it becomes very easy! Any language can 'implement' rotate
as something like:
rotate_inplace(a,shiftcount)
because the work has already been done. But the same 'blub' language as
above can also define the rotation like this:
proc rotate_left(&a,count)=
a:=right(a,-count) concat left(a,count)
end
(But it requires extra workspace compared with the plain code above.)
--
Bartc
Back to comp.programming | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Rotation Paul <pepstein5@gmail.com> - 2016-01-04 09:25 -0800
Re: Rotation David Brown <david.brown@hesbynett.no> - 2016-01-04 23:51 +0100
Re: Rotation BartC <bc@freeuk.com> - 2016-01-05 01:24 +0000
Re: Rotation Kaz Kylheku <kaz@kylheku.com> - 2016-01-05 04:59 +0000
Re: Rotation BartC <bc@freeuk.com> - 2016-01-05 12:22 +0000
Re: Rotation BartC <bc@freeuk.com> - 2016-01-05 12:55 +0000
Re: Rotation Kaz Kylheku <kaz@kylheku.com> - 2016-01-05 14:37 +0000
Re: Rotation BartC <bc@freeuk.com> - 2016-01-05 18:48 +0000
Re: Rotation Melzzzzz <mel@zzzzz.com> - 2016-01-05 09:13 +0100
csiph-web