-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-54.scm
42 lines (32 loc) · 1.13 KB
/
2-54.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#lang sicp
(define (fringe t)
(cond ((pair? t) (append (fringe (car t)) (fringe (cdr t))))
((null? t) '())
(else (list t))))
; solution without eq?
(define (flat-equal? left right)
(equal? (fringe left) (fringe right)))
(equal? '(this is a list) '(this is a list))
(flat-equal? '(this is a list) '(this (is a) list))
(define (accumulate op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))))
(define (accumulate-n op init seqs)
(if (null? (car seqs))
'()
(cons (accumulate op init (map car seqs))
(accumulate-n op init (map cdr seqs)))))
(define (transpose mat)
(accumulate-n cons '() mat))
(define (contains? condition list)
(cond ((not (pair? list)) #f)
((condition (car list)) #t)
(else (contains? condition (cdr list)))))
; soution with eq and matrix transpose
(define (equal-transpose left right)
(not (contains? (lambda (row)
(not (eq? (car row) (cadr row))))
(transpose (list (fringe left) (fringe right))))))
(equal-transpose '(this is a list) '(this (is a) list))