Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Scheme/Racket

Name: Anonymous 2012-11-27 17:53

/prog/, How do you do this shit?

Write a Scheme function palindrome-strings that consumes a leaf-labelled tree lt, where all labels are strings, and produces a list of the strings in lt that are palindromes. Any order is acceptable. (a string is a palindrome if its reverse is identical to it, for example: “wow” “abcddcba” ). Some examples follow:

(define myllt '( ("hat" ("cat" "dog" "wow") ("abcba" "Wow" "nice") ("halah" "lol")) ("was saw" "end")))
(define myllt2 '(("hat" ("cat" "dog" "woww") ("abcbda" "nice") ("hala" "lolo")) ("was see" "end")))
(define myllt3 '( "aaa" ("hat" ("cat" "dog" "woww") ("hala" "baab")) ("was see" "end")))
(define res1 '("wow" "abcba" "halah" "lol" "was saw"))
(palindrome-strings myllt) => res1
(palindrome-strings myllt2) => empty
(palindrome-strings myllt3) => (list "aaa" "baab")



;; A leaf-labelled tree llt is one of the following:
;; empty
;; (cons l1 l2) where
;; l1 is a non-empty llt and
;; l2 is a llt
;; (cons v l) where
;; v is a string and
;; l is a llt

Name: Anonymous 2012-11-27 17:57

FFS...

Name: Anonymous 2012-11-27 17:57

By reading SICP.

Name: Anonymous 2012-11-27 18:05

(defun palindrome (str)
       (when (string str)
         (if (equal str (reverse str))
         t)))

Name: Anonymous 2012-11-27 18:12

>>4
We're not allowed to use reverse at all.

Name: Anonymous 2012-11-27 18:12


(for/fold ([p '()]) ([a (flatten tre)])
  (if (palindrome? a) (cons a p) p))

Name: Anonymous 2012-11-27 18:19

...

Name: Anonymous 2012-11-27 18:29

>>5
How about appending?

Name: Anonymous 2012-11-27 22:12

>>5
Why?

Name: Anonymous 2012-11-28 10:24

>>9
Stupid course doesn't allow it for this assignment

Name: Anonymous 2012-11-28 10:27

>>10
Just write your own implementation of reverse.

Name: Anonymous 2012-11-28 10:41

Quick, use this mysterious function called 'sywort'!

(define (sywort l)
 (let vag ((l l) (r '()))
  (if (null? l) r
   (vag (cdr l)
    (cons (car l) r)))))

Name: Anonymous 2012-11-28 10:59

ENTERPRISE SCHEME:


(define (palindrome? string)
   (if (< (string-length string) 2)
       #t
       (and (equal? (string-ref string 0) (string-ref string (- (string-length string) 1)))
              (palindrome? (substring string 1 (- (string-length string) 1))))))

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List