unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: James Crake-Merani <james@jamescm.co.uk>
To: guile-user@gnu.org
Subject: Re: GPT-4 knows Guile! :)
Date: Sat, 18 Mar 2023 07:42:15 +0000	[thread overview]
Message-ID: <a1c77037-8ebb-ed17-c556-e6e9a7edb616@jamescm.co.uk> (raw)
In-Reply-To: <CADEOadfvqKy4J4+kubHNpPDgcZsNkgfgzDj_BicLqFeYXupcZA@mail.gmail.com>

Hi,

I think some students think that ChatGPT is this magical AI which can do 
their work for them, when in reality ChatGPT is really poor at writing 
code. According to the paper on ChatGPT 4[1], in easy Leetcode 
challenges, ChatGPT 4 does decently although 3.5 still does poorly with 
a score of 12/41 compared to 4's 31/41. However, when the difficulty was 
increased, ChatGPT only got 21/80 on the medium Leetcode, and a measly 
4/45 on the hard Leetcode. Results are even worse with ChatGPT 3.5, with 
it getting 8/80 on the medium challenges, and nothing on the hard 
challenges.

Anecdotally, one of my friends at university prompted it to write some 
code in Java. It started to come out with Python code. But that's 
alright, because the first line had 'import java'. Unfortunately, my 
friend had cut it off before it got any further so we didn't get to see 
what it came out with.

I think, at least for now, that our jobs are safe. I suspect 
potentially, AI like ChatGPT could help automate more trivial tasks so 
the humans can be left just with the tasks that are done best by humans. 
But at the moment, while ChatGPT has made improvements in this regard, 
it is still too erroneous to be used for even that purpose.

James Crake-Merani.

[1]: https://openai.com/research/gpt-4 (click 'read paper' for the 
actual paper.)

On 17/03/2023 19:28, Damien Mattei wrote:
> finally i can reconnect to chatGPT server and here is the result:
>
> (define (min-dnf expr)
>    (let* ((vars (variables expr))
>           (truth-table (generate-truth-table vars))
>           (rows (filter (lambda (row) (eval expr (make-environment vars
> row))) truth-table))
>           (clauses (map (lambda (row) (create-clause vars row)) rows))
>           (dnf (fold-right or #f clauses)))
>      (simplify dnf)))
>
> (define (variables expr)
>    (let ((vars '()))
>      (define (collect-variables exp)
>        (cond ((symbol? exp) (unless (memq exp vars) (set! vars (cons exp
> vars))))
>              ((pair? exp) (map collect-variables exp))))
>      (collect-variables expr)
>      vars))
>
> (define (generate-truth-table vars)
>    (let ((n (length vars)))
>      (map (lambda (i) (list->vector (reverse (map (lambda (b) (if b 1 0))
>                                                    (reverse (binary i
> n)))))) (range (expt 2 n)))))
>
> (define (binary n bits)
>    (let loop ((n n) (bits bits) (acc '()))
>      (if (zero? bits) acc
>          (let ((bit (remainder n 2)))
>            (loop (quotient n 2) (- bits 1) (cons bit acc))))))
>
> (define (create-clause vars row)
>    (let ((clause '()))
>      (define (create-term var value)
>        (if value var (cons 'not (list var))))
>      (map (lambda (pair) (set! clause (cons (create-term (car pair) (cdr
> pair)) clause)))
>           (map cons vars row))
>      (reverse clause)))
>
> (define (make-environment vars row)
>    (let ((env '()))
>      (map (lambda (pair) (set! env (cons pair env)))
>           (map cons vars row))
>      (reverse env)))
>
> (define (simplify expr)
>    (let ((simp (simplify-expr expr)))
>      (if (eq? expr simp) expr (simplify simp))))
>
> (define (simplify-expr expr)
>    (cond ((symbol? expr) expr)
>          ((and (pair? expr) (eq? (car expr) 'not) (pair? (cdr expr)))
>           (let ((arg (simplify-expr (cadr expr))))
>             (cond ((symbol? arg) (list 'not arg))
>                   ((and (pair? arg) (eq? (car arg) 'not))
>                    (simplify-expr (cadr arg)))
>                   (else (list 'not arg))))))
>          ((and (pair? expr) (memq (car expr) '(and or)))
>           (let ((op (car expr))
>                 (args (map simplify-expr (cdr expr))))
>             (let loop ((args args) (acc '()))
>               (if (null? args)
>                   (if (eq? op 'and) #t (fold-right or #f acc))
>                   (let ((arg (car args)))
>                     (if (eq? arg (if (eq? op 'and) #f #t))
>                         (if (eq? op 'and) #f (fold-right or #f acc))
>                         (loop (cdr args) (cons arg acc)))))))))
>          (else expr)))
>
> ;;; /Users/mattei/chatGPTminimaldisjunctivenormalform.scm:6:26: source
> expression failed to match any pattern in form or
> ice-9/boot-9.scm:1685:16: In procedure raise-exception:
> Syntax error:
> unknown location: source expression failed to match any pattern in form or
>
> Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
>
> and my conclusion is chatGPT is a very bad student that does not know the
> difference between a Scheme procedure and a special form :-)
>
> and the 65lines chatGPT code is far from my > 5000 lines code to minimize
> in disjunctive normal forms.......
>
> but you will continue to read a lot of newspaper saying AI is able to write
> programs....
>
> chatGPT is only a "predictor of the next word" ,okay perheaps a bit more....
>
> On Fri, Mar 17, 2023 at 8:09 PM Damien Mattei <damien.mattei@gmail.com>
> wrote:
>
>> i understand the fun thing... :-)
>>
>> and i did not check the program with Guile but asked a Racket version of
>> the bouncing ball and it is full of errors even if i try to correct them by
>> hand...
>>
>> Welcome to DrRacket, version 8.7 [cs].
>> Language: racket/gui, with debugging; memory limit: 8192 MB.
>> . . send: no such method
>>    method name: register-event-callback
>>    class name: canvas%
>>
>>
>> after i asked this question to chatGPT:
>>
>> "Can you write a Guile Scheme program that put a logical expression in
>> minimal disjunctive normal form?"
>>
>> i got a truncated program trying to code a QuineMcCluskey algorithm
>> implementation that i can not redisplay here because i got a connection
>> error to server and that looked like a bit,but from very very very far away
>> my own logic code in github that i spend many years to wrote,
>>
>> and the Chat finished  like this:
>>
>> "Hmm...something seems to have gone wrong. Maybe try me again in a little
>> bit"
>>
>> :-)
>>
>> Damien
>>
>> On Fri, Mar 17, 2023 at 6:26 PM Mikael Djurfeldt <mikael@djurfeldt.com>
>> wrote:
>>
>>> Can you write a Guile Scheme program which displays a bouncing ball?
>>>
>>> As an AI language model, I cannot create a full graphical program directly
>>> within this text-based environment. However, I can provide you with an
>>> example of a simple bouncing ball simulation using the Guile Scheme
>>> programming language and the SDL2 (Simple DirectMedia Layer) library,
>>> which
>>> is commonly used for creating graphical applications.
>>>
>>> To start, you'll need to have Guile and the Guile-SDL2 bindings installed.
>>> The following instructions are for a Debian-based system:
>>>
>>>     1. Install Guile:
>>>
>>> csharp
>>>
>>> sudo apt-get install guile-2.2
>>>
>>>
>>>     1. Install SDL2 development libraries:
>>>
>>> csharp
>>>
>>> sudo apt-get install libsdl2-dev libsdl2-image-dev libsdl2-ttf-dev
>>>
>>>
>>>     1. Clone and build the Guile-SDL2 library:
>>>
>>> bash
>>>
>>> git clone https://github.com/davexunit/guile-sdl2.gitcd guile-sdl2
>>> autoreconf -vif
>>> ./configure
>>> make
>>> sudo make install
>>>
>>> Now you can create the bouncing ball program in a file called
>>> bouncing-ball.scm:
>>>
>>> scheme
>>>
>>> (use-modules (sdl2)
>>>               (sdl2 video)
>>>               (sdl2 events)
>>>               (sdl2 timer)
>>>               (sdl2 rect))
>>>
>>> (define screen-width 640)
>>> (define screen-height 480)
>>> (define ball-size 20)
>>>
>>> (define ball-x (make-parameter (/ (- screen-width ball-size) 2)))
>>> (define ball-y (make-parameter (/ (- screen-height ball-size) 2)))
>>> (define ball-x-velocity (make-parameter 4))
>>> (define ball-y-velocity (make-parameter 4))
>>>
>>> (define (process-events)
>>>    (let loop ((event (poll-event)))
>>>      (when event
>>>        (case (event-type event)
>>>          ((quit) #f)
>>>          (else (loop (poll-event)))))))
>>>
>>> (define (update-ball)
>>>    (parameterize ((x (ball-x))
>>>                   (y (ball-y))
>>>                   (vx (ball-x-velocity))
>>>                   (vy (ball-y-velocity)))
>>>      (ball-x (+ x vx))
>>>      (ball-y (+ y vy))
>>>
>>>      (when (or (<= (+ x vx) 0) (>= (+ x vx ball-size) screen-width))
>>>        (ball-x-velocity (- vx)))
>>>
>>>      (when (or (<= (+ y vy) 0) (>= (+ y vy ball-size) screen-height))
>>>        (ball-y-velocity (- vy)))))
>>>
>>> (define (draw-ball renderer)
>>>    (let ((ball-rect (make-rect (ball-x) (ball-y) ball-size ball-size)))
>>>      (set-render-draw-color renderer 255 0 0 255)
>>>      (render-fill-rect renderer ball-rect)))
>>>
>>> (define (main)
>>>    (init! +init-video+)
>>>    (let ((window (create-window "Bouncing Ball" +window-pos-undefined+
>>> +window-pos-undefined+ screen-width screen-height +window-shown+))
>>>          (renderer (create-renderer window -1 +renderer-accelerated+)))
>>>      (set-render-draw-color renderer 0 0 0 255)
>>>      (let loop ()
>>>        (unless (process-events)
>>>          (update-ball)
>>>          (render-clear renderer)
>>>          (draw-ball renderer)
>>>          (render-present renderer)
>>>          (delay 16)
>>>          (loop)))))
>>>
>>> (main)
>>>
>>> This example sets up a window using SDL2 and creates a simple ball that
>>> bounces off the edges of the window. You can run the program using the
>>> following command:
>>>
>>> bash
>>>
>>> guile bouncing-ball.scm
>>>
>>> Keep in mind that this is a simple example, and there are many ways to
>>> improve or expand upon it, such as using images
>>> [image: mikael@djurfeldt.com]
>>> I get the following error message: ;;; note: auto-compilation is enabled,
>>> set GUILE_AUTO_COMPILE=0 ;;; or pass the --no-auto-compile argument to
>>> disable. ;;; compiling /home/mdj/guile/bouncing-ball.scm ;;;
>>> /home/mdj/guile/bouncing-ball.scm:19:13: warning: possibly unbound
>>> variable
>>> `event-type' ;;; /home/mdj/guile/bouncing-ball.scm:24:18: warning:
>>> possibly
>>> unbound variable `x' ;;; /home/mdj/guile/bouncing-ball.scm:25:18: warning:
>>> possibly unbound variable `y' ;;; /home/mdj/guile/bouncing-ball.scm:26:18:
>>> warning: possibly unbound variable `vx' ;;;
>>> /home/mdj/guile/bouncing-ball.scm:27:18: warning: possibly unbound
>>> variable
>>> `vy' ;;; /home/mdj/guile/bouncing-ball.scm:39:5: warning: possibly unbound
>>> variable `set-render-draw-color' ;;;
>>> /home/mdj/guile/bouncing-ball.scm:40:5: warning: possibly unbound variable
>>> `render-fill-rect' ;;; /home/mdj/guile/bouncing-ball.scm:43:3: warning:
>>> possibly unbound variable `init!' ;;;
>>> /home/mdj/guile/bouncing-ball.scm:43:9: warning: possibly unbound variable
>>> `+init-video+' ;;; /home/mdj/guile/bouncing-ball.scm:44:17: warning:
>>> possibly unbound variable `create-window' ;;;
>>> /home/mdj/guile/bouncing-ball.scm:44:47: warning: possibly unbound
>>> variable
>>> `+window-pos-undefined+' ;;; /home/mdj/guile/bouncing-ball.scm:44:120:
>>> warning: possibly unbound variable `+window-shown+' ;;;
>>> /home/mdj/guile/bouncing-ball.scm:45:19: warning: possibly unbound
>>> variable
>>> `create-renderer' ;;; /home/mdj/guile/bouncing-ball.scm:45:35: warning:
>>> possibly unbound variable `window' ;;;
>>> /home/mdj/guile/bouncing-ball.scm:45:45: warning: possibly unbound
>>> variable
>>> `+renderer-accelerated+' ;;; /home/mdj/guile/bouncing-ball.scm:50:9:
>>> warning: possibly unbound variable `render-clear' ;;;
>>> /home/mdj/guile/bouncing-ball.scm:52:9: warning: possibly unbound variable
>>> `render-present' ;;; compiled
>>>
>>> /home/mdj/.cache/guile/ccache/3.0-LE-8-4.6/home/mdj/guile/bouncing-ball.scm.go
>>> Backtrace: In ice-9/boot-9.scm: 1752:10 6 (with-exception-handler _ _
>>> #:unwind? _ # _) In unknown file: 5 (apply-smob/0 #<thunk 7f79f1cd8300>)
>>> In
>>> ice-9/boot-9.scm: 724:2 4 (call-with-prompt _ _ #<procedure
>>> default-prompt-handle…>) In ice-9/eval.scm: 619:8 3 (_ #(#(#<directory
>>> (guile-user) 7f79f1cdbc80>))) In ice-9/boot-9.scm: 2836:4 2
>>> (save-module-excursion _) 4388:12 1 (_) In
>>> /home/mdj/guile/bouncing-ball.scm: 43:3 0 (main)
>>> /home/mdj/guile/bouncing-ball.scm:43:3: In procedure main: Unbound
>>> variable: init!
>>>
>>> Apologies for the confusion. It seems I made a mistake in the provided
>>> code, and some symbols were not correctly imported. Let's fix the imports
>>> by using the appropriate modules and functions. Replace the (use-modules
>>> ...) line in the code with the following:
>>>
>>> scheme
>>>
>>> (use-modules (sdl2) (sdl2 video) (sdl2 events) (sdl2 timer) (sdl2
>>> rect) (sdl2 rwops) (sdl2 surface) (sdl2 sdl))
>>>
>>> Additionally, it seems that the init! function is not being recognized. It
>>> should be SDL_Init instead. So, replace the line (init! +init-video+)
>>> with:
>>>
>>> scheme
>>>
>>> (SDL_Init +SDL_INIT_VIDEO+)
>>>
>>> After making these changes, the updated bouncing-ball.scm should look like
>>> this:
>>>
>>> scheme
>>>
>>> (use-modules (sdl2)
>>>               (sdl2 video)
>>>               (sdl2 events)
>>>               (sdl2 timer)
>>>               (sdl2 rect)
>>>               (sdl2 rwops)
>>>               (sdl2 surface)
>>>               (sdl2 sdl))
>>>
>>> (define screen-width 640)
>>> (define screen-height 480)
>>> (define ball-size 20)
>>>
>>> (define ball-x (make-parameter (/ (- screen-width ball-size) 2)))
>>> (define ball-y (make-parameter (/ (- screen-height ball-size) 2)))
>>> (define ball-x-velocity (make-parameter 4))
>>> (define ball-y-velocity (make-parameter 4))
>>>
>>> (define (process-events)
>>>    (let loop ((event (poll-event)))
>>>      (when event
>>>        (case (event-type event)
>>>          ((quit) #f)
>>>          (else (loop (poll-event)))))))
>>>
>>> (define (update-ball)
>>>    (parameterize ((x (ball-x))
>>>                   (y (ball-y))
>>>                   (vx (ball-x-velocity))
>>>                   (vy (ball-y-velocity)))
>>>      (ball-x (+ x vx))
>>>      (ball-y (+ y vy))
>>>
>>>      (when (or (<= (+ x vx) 0) (>= (+ x vx ball-size) screen-width))
>>>        (ball-x-velocity (- vx)))
>>>
>>>      (when (or (<= (+ y vy) 0) (>= (+ y vy ball-size) screen-height))
>>>        (ball-y-velocity (- vy)))))
>>>
>>> (define (draw-ball renderer)
>>>    (let ((ball-rect (make-rect (ball-x) (ball-y) ball-size ball-size)))
>>>      (set-render-draw-color renderer 255 0 0 255)
>>>      (render-fill-rect renderer ball-rect)))
>>>
>>> (define (main)
>>>    (SDL_Init +SDL_INIT_VIDEO+)
>>>    (let ((window (create-window "Bouncing Ball" +window-pos-undefined+
>>> +window-pos-undefined+ screen-width screen-height +window-shown+))
>>>          (renderer (create-renderer window -1 +renderer-accelerated+)))
>>>      (set-render-draw-color renderer 0 0 0 255)
>>>      (let loop ()
>>>        (unless (process-events)
>>>          (update-ball)
>>>          (render-clear renderer)
>>>          (draw-ball renderer)
>>>          (render-present renderer)
>>>          (delay 16)
>>>          (loop)))))
>>>
>>> (main)
>>>
>>> Now, the program should run without any issues. Run it with:
>>>
>>> bash
>>>
>>> guile bouncing-ball.scm
>>>
>>> [image: mikael@djurfeldt.com]
>>> I think it should be make-window instead of create-window.
>>>
>>> You are correct. The function should be make-window instead of
>>> create-window.
>>> I apologize for the confusion. Here's the updated bouncing-ball.scm code
>>> with the corrected function name:
>>>
>>> scheme
>>>
>>> (use-modules (sdl2)
>>>               (sdl2 video)
>>>               (sdl2 events)
>>>               (sdl2 timer)
>>>               (sdl2 rect)
>>>               (sdl2 rwops)
>>>               (sdl2 surface)
>>>               (sdl2 sdl))
>>>
>>> (define screen-width 640)
>>> (define screen-height 480)
>>> (define ball-size 20)
>>>
>>> (define ball-x (make-parameter (/ (- screen-width ball-size) 2)))
>>> (define ball-y (make-parameter (/ (- screen-height ball-size) 2)))
>>> (define ball-x-velocity (make-parameter 4))
>>> (define ball-y-velocity (make-parameter 4))
>>>
>>> (define (process-events)
>>>    (let loop ((event (poll-event)))
>>>      (when event
>>>        (case (event-type event)
>>>          ((quit) #f)
>>>          (else (loop (poll-event)))))))
>>>
>>> (define (update-ball)
>>>    (parameterize ((x (ball-x))
>>>                   (y (ball-y))
>>>                   (vx (ball-x-velocity))
>>>                   (vy (ball-y-velocity)))
>>>      (ball-x (+ x vx))
>>>      (ball-y (+ y vy))
>>>
>>>      (when (or (<= (+ x vx) 0) (>= (+ x vx ball-size) screen-width))
>>>        (ball-x-velocity (- vx)))
>>>
>>>      (when (or (<= (+ y vy) 0) (>= (+ y vy ball-size) screen-height))
>>>        (ball-y-velocity (- vy)))))
>>>
>>> (define (draw-ball renderer)
>>>    (let ((ball-rect (make-rect (ball-x) (ball-y) ball-size ball-size)))
>>>      (set-render-draw-color renderer 255 0 0 255)
>>>      (render-fill-rect renderer ball-rect)))
>>>
>>> (define (main)
>>>    (SDL_Init +SDL_INIT_VIDEO+)
>>>    (let ((window (make-window "Bouncing Ball" +window-pos-undefined+
>>> +window-pos-undefined+ screen-width screen-height +window-shown+))
>>>          (renderer (create-renderer window -1 +renderer-accelerated+)))
>>>      (set-render-draw-color renderer 0 0 0 255)
>>>      (let loop ()
>>>        (unless (process-events)
>>>          (update-ball)
>>>          (render-clear renderer)
>>>          (draw-ball renderer)
>>>          (render-present renderer)
>>>          (delay 16)
>>>          (loop)))))
>>>
>>> (main)
>>>
>>> Now, the program should run as expected:
>>>
>>> bash
>>>
>>> guile bouncing-ball.scm
>>>
>>> [image: mikael@djurfeldt.com]
>>> Please write the program according to the guile-sdl2 manual.
>>>
>>> Apologies for the confusion. Here's an updated version of the bouncing
>>> ball
>>> program according to the guile-sdl2 manual:
>>>
>>> scheme
>>>
>>> (use-modules (sdl2)
>>>               (sdl2 video)
>>>               (sdl2 events)
>>>               (sdl2 timer)
>>>               (sdl2 rect)
>>>               (sdl2 rwops)
>>>               (sdl2 surface)
>>>               (sdl2 sdl))
>>>
>>> (define screen-width 640)
>>> (define screen-height 480)
>>> (define ball-size 20)
>>>
>>> (define ball-x (make-parameter (/ (- screen-width ball-size) 2)))
>>> (define ball-y (make-parameter (/ (- screen-height ball-size) 2)))
>>> (define ball-x-velocity (make-parameter 4))
>>> (define ball-y-velocity (make-parameter 4))
>>>
>>> (define (process-events)
>>>    (let loop ((event (SDL_PollEvent)))
>>>      (when event
>>>        (case (event-type event)
>>>          ((quit) #f)
>>>          (else (loop (SDL_PollEvent)))))))
>>>
>>> (define (update-ball)
>>>    (parameterize ((x (ball-x))
>>>                   (y (ball-y))
>>>                   (vx (ball-x-velocity))
>>>                   (vy (ball-y-velocity)))
>>>      (ball-x (+ x vx))
>>>      (ball-y (+ y vy))
>>>
>>>      (when (or (<= (+ x vx) 0) (>= (+ x vx ball-size) screen-width))
>>>        (ball-x-velocity (- vx)))
>>>
>>>      (when (or (<= (+ y vy) 0) (>= (+ y vy ball-size) screen-height))
>>>        (ball-y-velocity (- vy)))))
>>>
>>> (define (draw-ball renderer)
>>>    (let ((ball-rect (make-rect (ball-x) (ball-y) ball-size ball-size)))
>>>      (SDL_SetRenderDrawColor renderer 255 0 0 255)
>>>      (SDL_RenderFillRect renderer ball-rect)))
>>>
>>> (define (main)
>>>    (SDL_Init +SDL_INIT_VIDEO+)
>>>    (let ((window (SDL_CreateWindow "Bouncing Ball"
>>> +SDL_WINDOWPOS_UNDEFINED+ +SDL_WINDOWPOS_UNDEFINED+ screen-width
>>> screen-height +SDL_WINDOW_SHOWN+))
>>>          (renderer (SDL_CreateRenderer window -1
>>> +SDL_RENDERER_ACCELERATED+)))
>>>      (SDL_SetRenderDrawColor renderer 0 0 0 255)
>>>      (let loop ()
>>>        (unless (process-events)
>>>          (update-ball)
>>>          (SDL_RenderClear renderer)
>>>          (draw-ball renderer)
>>>          (SDL_RenderPresent renderer)
>>>          (SDL_Delay 16)
>>>          (loop))
>>>      (SDL_DestroyRenderer renderer)
>>>      (SDL_DestroyWindow window)))
>>>
>>> (SDL_Quit)
>>> (main)
>>>
>>> Now, the program should run as expected:
>>>
>>> bash
>>>
>>> guile bouncing-ball.scm
>>>
>>> [image: mikael@djurfeldt.com]
>>> But all those SDL_ calls look like C bindings. Please use guile-sdl2
>>> bindings.
>>>
>>> I apologize for the confusion. Here's the bouncing ball program
>>> implemented
>>> using the Guile-SDL2 bindings:
>>>
>>> scheme
>>>
>>> (use-modules (sdl2)
>>>               (sdl2 video)
>>>               (sdl2 events)
>>>               (sdl2 timer)
>>>               (sdl2 rect)
>>>               (sdl2 rwops)
>>>               (sdl2 surface)
>>>               (sdl2 sdl))
>>>
>>> (define screen-width 640)
>>> (define screen-height 480)
>>> (define ball-size 20)
>>>
>>> (define ball-x (make-parameter (/ (- screen-width ball-size) 2)))
>>> (define ball-y (make-parameter (/ (- screen-height ball-size) 2)))
>>> (define ball-x-velocity (make-parameter 4))
>>> (define ball-y-velocity (make-parameter 4))
>>>
>>> (define (process-events)
>>>    (let loop ((event (poll-event)))
>>>      (when event
>>>        (case (event-type event)
>>>          ((quit) #f)
>>>          (else (loop (poll-event)))))))
>>>
>>> (define (update-ball)
>>>    (parameterize ((x (ball-x))
>>>                   (y (ball-y))
>>>                   (vx (ball-x-velocity))
>>>                   (vy (ball-y-velocity)))
>>>      (ball-x (+ x vx))
>>>      (ball-y (+ y vy))
>>>
>>>      (when (or (<= (+ x vx) 0) (>= (+ x vx ball-size) screen-width))
>>>        (ball-x-velocity (- vx)))
>>>
>>>      (when (or (<= (+ y vy) 0) (>= (+ y vy ball-size) screen-height))
>>>        (ball-y-velocity (- vy)))))
>>>
>>> (define (draw-ball renderer)
>>>    (let ((ball-rect (make-rect (ball-x) (ball-y) ball-size ball-size)))
>>>      (set-render-draw-color! renderer 255 0 0 255)
>>>      (render-fill-rect! renderer ball-rect)))
>>>
>>> (define (main)
>>>    (init! +init-video+)
>>>    (let ((window (make-window "Bouncing Ball" +window-pos-undefined+
>>> +window-pos-undefined+ screen-width screen-height +window-shown+))
>>>          (renderer (make-renderer window -1 +renderer-accelerated+)))
>>>      (set-render-draw-color! renderer 0 0 0 255)
>>>      (let loop ()
>>>        (unless (process-events)
>>>          (update-ball)
>>>          (render-clear! renderer)
>>>          (draw-ball renderer)
>>>          (render-present! renderer)
>>>          (delay 16)
>>>          (loop))
>>>      (destroy-renderer! renderer)
>>>      (destroy-window! window)))
>>>
>>> (quit!)
>>> (main)
>>>
>>> Now, the program should run as expected:
>>>
>>> bash
>>>
>>> guile bouncing-ball.scm
>>>
>>> ---
>>>
>>> Turns out there are multiple problems with that program also. Here I
>>> (mikael) fixed a number of problems and actually *got* a bouncing
>>> ball. :)
>>>
>>> (use-modules (sdl2)
>>>               (sdl2 video)
>>>               (sdl2 events)
>>>               ;(sdl2 timer)
>>>               (sdl2 rect)
>>>               ;(sdl2 rwops)
>>>               (sdl2 surface)
>>>                                          ;(sdl2 sdl)
>>>               (sdl2 render))
>>>
>>> (define screen-width 640)
>>> (define screen-height 480)
>>> (define ball-size 20)
>>>
>>> (define ball-x (make-parameter (/ (- screen-width ball-size) 2)))
>>> (define ball-y (make-parameter (/ (- screen-height ball-size) 2)))
>>> (define ball-x-velocity (make-parameter 4))
>>> (define ball-y-velocity (make-parameter 4))
>>>
>>> (define (process-events)
>>>    (let loop ((event (poll-event)))
>>>      (if event
>>>          (cond
>>>           ((quit-event? event) #t)
>>>           (else (loop (poll-event))))
>>>          #f)))
>>>
>>> (define (update-ball)
>>>    (let ((x (ball-x))
>>>          (y (ball-y))
>>>          (vx (ball-x-velocity))
>>>          (vy (ball-y-velocity)))
>>>      (ball-x (+ x vx))
>>>      (ball-y (+ y vy))
>>>
>>>      (when (or (<= (+ x vx) 0) (>= (+ x vx ball-size) screen-width))
>>>        (ball-x-velocity (- vx)))
>>>
>>>      (when (or (<= (+ y vy) 0) (>= (+ y vy ball-size) screen-height))
>>>        (ball-y-velocity (- vy)))))
>>>
>>> (define (draw-ball renderer)
>>>    (let ((ball-rect (make-rect (ball-x) (ball-y) ball-size ball-size)))
>>>      (set-renderer-draw-color! renderer 255 0 0 255)
>>>      (render-fill-rect renderer ball-rect)))
>>>
>>> (define (main)
>>>    (sdl-init)
>>>    (let* ((window (make-window #:title "Bouncing Ball" #:size (list
>>> screen-width screen-height)))
>>>           (renderer (make-renderer window)))
>>>      (set-renderer-draw-color! renderer 0 0 0 255)
>>>      (let loop ()
>>>        (unless (process-events)
>>>          (update-ball)
>>>          (set-renderer-draw-color! renderer 0 0 0 0)
>>>          (clear-renderer renderer)
>>>          (draw-ball renderer)
>>>          (present-renderer renderer)
>>>                                          ;(delay 16)
>>>          (loop))
>>>      (delete-renderer! renderer)
>>>                                          ;(destroy-window! window)
>>>      )))
>>>
>>> ;(quit!)
>>> (main)
>>>



  reply	other threads:[~2023-03-18  7:42 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-17 16:01 GPT-4 knows Guile! :) Mikael Djurfeldt
2023-03-17 17:25 ` Mikael Djurfeldt
2023-03-17 19:09   ` Damien Mattei
2023-03-17 19:28     ` Damien Mattei
2023-03-18  7:42       ` James Crake-Merani [this message]
2023-03-18  8:10         ` Mikael Djurfeldt
2023-03-18  8:29         ` tomas
2023-03-18 10:52           ` James Crake-Merani
2023-03-18 12:00             ` Mikael Djurfeldt
2023-03-18 12:03               ` Mikael Djurfeldt
2023-03-19  8:42                 ` tomas
2023-03-19 23:18                   ` Dr. Arne Babenhauserheide
2023-03-20  1:39                     ` Nala Ginrut
2023-03-20  4:53                       ` Dr. Arne Babenhauserheide
2023-03-20  8:04                         ` tomas
2023-03-20  8:00                     ` tomas
2023-03-20 10:56                     ` Zelphir Kaltstahl
2023-03-20 11:20                       ` tomas
     [not found]   ` <ZBViQz4I3E4Qb08Q@tuxteam.de>
     [not found]     ` <CAA2XvwJY1mX9rjVvwL3yATSt1+f86JkdkEFa55L8g4rrZ_tX5A@mail.gmail.com>
     [not found]       ` <ZBVyyU8xGng2imNB@tuxteam.de>
     [not found]         ` <CAA2XvwJmCe2DNc+45S8yMtHFhNYZBRS5ukjnZxWzDtt9iT1mmw@mail.gmail.com>
     [not found]           ` <ZBV3/e97xfI14i/M@tuxteam.de>
     [not found]             ` <CAA2XvwJxuECNYf3M+ako3KS8j8OGG6caYVSQY1ygiXaTWxSY0g@mail.gmail.com>
     [not found]               ` <ZBV6eP4DUuBlTymg@tuxteam.de>
     [not found]                 ` <CAA2XvwLOqBr1cxC+ZpVOXdw7Tto7hZCifUO7mFF6wM071T3Dqg@mail.gmail.com>
     [not found]                   ` <CAA2Xvw+pTXC2RO-epuiV-+ChaNWzrdQ4n_ATwutkQ3n7sq5GBw@mail.gmail.com>
2023-03-18  9:23                     ` Damien Mattei

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a1c77037-8ebb-ed17-c556-e6e9a7edb616@jamescm.co.uk \
    --to=james@jamescm.co.uk \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).