​​
Exercise 336. Is (bundle "abc" 0) a proper use of the bundle function? What
does it produce? Why?


​Solution. It's not a proper use. It produces nothing. It doesn't
terminate. Because (drop ls 0)​ returns a list not smaller than ls, hence
the recursion of bundle doesn't reach the base case.

I felt like adding an error case to bundle.

; [List-of 1String] n -> [List-of String]
; produces a list of n-string from a list of 1-string

; exercise 336
; an error case, because you cannot group a nonempty list into groups of 0
(check-error (bundle (explode "mumble") 0))

(define (bundle ls n)
  (cond
    [(empty? ls) empty]
    [(= n 0) (error "0-bundles cannot represent a nonempty list")]
    [else (cons (implode (take ls n)) (bundle (drop ls n) n))]))

End of solution.

​​
Exercise 337. Define the function list->chunks. It consumes a list l of
arbitrary data and a natural number n. The function’s result is a list of
list chunks of size n. Each chunk represents a sub-sequence of items in l.

Use list->chunks to define bundle via function composition.


​Solution. To define bundle via function composition, I used map. I don't
know if that was the desired answer. I called it bundle-string. I think
bundle-string is really partition of exercise 338, so I'm not understanding
either this last bit of 337 or the entire point of 338.

​; exercise 337
; ls n -> [List-of [List-of n-chunks]]
; produces a list of lists of chunks
; where each chunk represents a subsequence of items in ls

; boundaries
(check-expect (list->chunks empty 3) empty)
(check-expect (list->chunks empty 0) empty)

; usual case
(check-expect (list->chunks '(a b c d e f g) 2) (list '(a b) '(c d) '(e f)
'(g)))

(define (list->chunks ls n)
  (cond
    [(or (= n 0) (empty? ls)) empty]
    [else (cons (take ls n) (list->chunks (drop ls n) n))]))

; bundle via list->chunks
(check-expect (bundle-string (explode "mumble") 2) (bundle-string (explode
"mumble") 2))

(define (bundle-string ls n)
  (map implode (list->chunks ls n)))
​
End of solution.

Exercise 338. Define the function partition. It consumes a String s and a
natural number n. The function produces a list of string chunks of size n.

For non-empty strings s and positive natural numbers n,

(equal? (partition s n) (bundle (explode s) n))

is true. But don’t use this equality as the definition for partition; use
substring instead.

Hint Have partition produce its “natural” result for the empty string. For
the case wheren is 0, see exercise 336.

Note The partition function is somewhat closer to what a cooperative
DrRacket environment would need than bundle.


I couldn't do this exercise. I can't use that equality as the definition
for partition. I must use substring. But to me the definition of partition
is my bundle-string above, which doesn't use substring and I think I'm
missing the whole point of partition. I can't see how substring could help
me. And even if it could, I think it would be very unclear, because to me
the clearest thing is to use map, since I want to implode each chunk in the
list.

Thank you for your attention.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to