In: Computer Science
(dominoes N) which returns a list containing the (N+1)(N+2)/2 tiles in a double-N domino set, with each tile represented as a dotted pair (an improper list).
(dominoes 2) ⇒ ((2 . 2) (2 . 1) (2 . 0) (1 . 1) (1 . 0) (0 . 0))
using the programming language scheme
i need the question to be answered using the programming language Scheme
Code:
(define (domineos n)
(cond ((= n 0) (cons (cons n n) '())) ;;if n = 0 returns '((0 .
0))
(else (append (helper n n) (domineos (- n 1)))))) ;;else appends
two list of pairs
(define (helper n count) ;;Helper_Function Returns list of pairs.
ex:(helper 2 2) returns ((2 . 2) (2 . 1) (2 . 0))
(cond ((= count 0) (cons (cons n 0) '())) ;;if count = 0 then
return '((n . 0))
(else (append (cons (cons n count) '()) (helper n (- count 1))))))
;;else append lists and continue
Snapshot of Code and Output: