HTDP2e Exercise 309

41 views
Skip to first unread message

Aron Zvi

unread,
Mar 5, 2020, 7:56:12 AM3/5/20
to Racket Users
Hey Guys,

I do not see how to apply pattern matching to this problem. 
Using for/list as below makes the most sense to me and I do not see how to fit in pattern matching

; [List-of [List-of String]] -> [List-of Natural]
; produces list of number of Strings per item in a llos
(check-expect (words-on-line empty) empty)
(check-expect (words-on-line (list empty)) (list 0))
(check-expect (words-on-line (list (list "22" "rr" "ff") (list "22" "rr"))) (list 3 2))

(define (words-on-line llos)
  (for/list ((los llos))
    (length los)))

Can I get a hint?

Daniel Prager

unread,
Mar 5, 2020, 4:01:46 PM3/5/20
to Aron Zvi, Racket Users
Hint:

(define (words-on-line llos)
  (match llos
      ['() empty]
      [(list a b ...) <recursive expression goes here>))

Dan

Aron Zvi

unread,
Mar 6, 2020, 2:05:21 AM3/6/20
to Racket Users
Thanks. I initially tried as below with first pattern [empty empty] and empty apparently is a variable that will match a non-empty list. I need to use  '() 

(define (words-on-line llos)
  (match llos
      [empty empty]

      [(list a b ...) <recursive expression goes here>))

Reply all
Reply to author
Forward
0 new messages