var RowName = new HtmlTableRow();
var Cell1Name = new HtmlTableCell();
var Cell2Name = new HtmlTableCell();
RowName.Cells.Add(Cell1Name);
RowName.Cells.Add(Cell2Name);
I need to create a function that swaps the pairs of elements in a scheme list. This is what I have come up with so far but I get error with the (define (interchange list)
(if (empty? list)
list
(interchange (append (car (cdr list) X)))))
empty?
答案 0 :(得分:3)
Try this:
var div = document.getElementsByTagName('div');
div.addEventListener('click', funcOne);
function funcOne(){
div.style.backgroundColor = "blue";
div.removeEventListener('click', funcOne);
div.addEventListener('click', funcTwo);
}
function funcTwo(){
div.style.backgroundColor = "red";
div.removeEventListener('click', funcTwo);
div.addEventListener('click', funcOne);
}
The trick is to process two elements per iteration, being careful with the edge cases. It works as expected for the sample input:
(define (interchange lst)
(if (or (null? lst) (null? (cdr lst))) ; if the list has 0 or 1 elements left
lst ; then return the list, we're done
(cons (cadr lst) ; otherwise grab the second element
(cons (car lst) ; and the first element
(interchange (cddr lst)))))) ; and move forward two elements
答案 1 :(得分:2)
#lang racket
(define (interchange xs)
(match xs
[(or '() (list _)) ; if the list is empty or has one element
xs] ; then interchange keeps the original list
[(list* x y more) ; if the list has two or more elements,
(list* y x (interchange more))])) ; then the result consists of the two
; ; first elements (with the order swapped)
; ; followed by the result of
; ; interchanging the remaining elements.
(interchange '( ))
(interchange '(a))
(interchange '(a b))
(interchange '(a b c))
(interchange '(a 1 b 2 c 3 d 4))
(interchange '(hello you -12.34 5 -6 enough))
输出:
'()
'(a)
'(b a)
'(b a c)
'(1 a 2 b 3 c 4 d)
'(you hello 5 -12.34 enough -6)