Advertisement
not-just-yeti

racket help for facebook question

Sep 12th, 2021
2,934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 3.41 KB | None | 0 0
  1. #lang racket
  2.  
  3. #;(define (boucle-musik x)
  4.         (let*((n 0))  
  5.     (letrec ((loop (lambda ()
  6.                      (cond((> n 5)
  7.                         (begin                  
  8.                           (set! n 1)
  9.                           (play (list-ref play-liste1 n))
  10.                           (loop)))
  11.                         ((begin
  12.                           (set! n (+ n 1))
  13.                           (play (list-ref play-liste1 n))
  14.                           (loop))))))))))
  15.  
  16. ; "Why doesn't this work?
  17.  
  18. ; Hi C.C.:
  19. ; a couple of syntax errors.
  20. ; First I'll re-do your code but use some different parens.
  21. ; (Note that racket views all parens as identical, so long as they match -- e.g. `[+ 2 {* 3 4}]`
  22. ;
  23. #;(define (boucle-musik x)
  24.   (let* {[n 0]}  
  25.     (letrec {[loop (lambda ()
  26.                      (cond[(> n 5) ; first cond-question
  27.                            (begin  ; first cond-answer    
  28.                              (set! n 1)
  29.                              (play (list-ref play-liste1 n))
  30.                              (loop))]
  31.                           [ ; second cond-question missing!
  32.                            (begin  ; second cond-answer
  33.                              (set! n (+ n 1))
  34.                              (play (list-ref play-liste1 n))
  35.                              (loop))]))
  36.                    ] ; end the binding for `loop`
  37.              } ; only one letrec binding
  38.       ; No body for 'letrec' here
  39.       )))
  40.  
  41. ; So the first syntax error, as DrRacket reports, is that 'letrec' has no body.
  42. ; Presumably you meant to call `(loop)` there
  43.  
  44. ; Second syntax error is that the second cond-question is missing.
  45. ; You could use `else`.
  46.  
  47.  
  48. ; Third, there's an idiomatic preference to avoid mutation.
  49. ; If you want to have n start as 0, and increment on each call to `loop`,
  50. ; just make it a parameter to `loop`.
  51. ; Putting those together, I get:
  52.  
  53. (define (boucle-musik-v2 play-liste1)  ;<-- I presume the play-list should be passed in
  54.     (letrec {[loop (lambda (n)
  55.                      (cond[(> n 5) ; first cond-question
  56.                            (begin  ; first cond-answer    
  57.                              (play (list-ref play-liste1 n))
  58.                              (loop 1))]
  59.                           [ ; second cond-question missing!
  60.                            (begin  ; second cond-answer
  61.                              (play (list-ref play-liste1 n))
  62.                              (loop (+ n 1)))]))
  63.                    ] ; end the binding for `loop`
  64.              } ; only one letrec binding
  65.       (loop 0)  ; the letrec body
  66.       ))
  67.  
  68. ; Finally, we can factor out the calls to `play`,
  69. ; and take advantage that lambda-bodies have an implicit `begin` in racket:
  70. ;
  71. (define (boucle-musik-v3 play-liste1)
  72.     (letrec {[loop (lambda (n)
  73.                      (play (list-ref play-liste1 n))
  74.                      (loop (cond [(> n 5) 1]
  75.                                  [else (+ 1 n)])))]}
  76.       (loop 0)  ; the letrec body
  77.       ))
  78.  
  79. ; and post-finally, some cool new syntax:
  80. ; "named let" is a way to make a function whose parameters are
  81. ; the variables-you're-declaring.
  82. ; So I'd write the above as:
  83. ;
  84. (define (boucle-musik-v4 play-liste1)
  85.     (let my-play-loop {[n 0]}
  86.       (play (list-ref play-liste1 n))
  87.       (my-play-loop (if (> n 5) 1 (+ 1 n)))))
  88.  
  89.  
  90. (define play-list1 '(a b c d e f g))
  91. (define (play sng) (printf "playing ~v~n" sng))
  92.  
  93. (boucle-musik-v4 '(a b c d e f g))
  94.  
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement