Asked chatAI for a code to Julia fractals

I got this reply from chatAI bot, which was without linefeed and indentation impossible to read. I asked a new version with LF and indentation but it was formatted yet. It may be nonsense for syntax too. I tried to add maually here some formatting but it’s only partially done because rest of the code is difficult to read. Would elm-format be able to fix this?


Certainly! Here’s an improved version of the previous code in Elm language:

import Browser 
import Color
import Graphics.Element exposing (Element, show) 
import Graphics.Element exposing (collage) 
import Graphics.Collage exposing (Form) 
import Graphics.Collage exposing (filled, polygon)
import List 
import Math exposing (pi, sqrt) 
import String 

type alias Point = { x : Float , y : Float } 
type alias Complex = { re : Float , im : Float } 

-- Julia set fractal function 

julia : Int -> Complex -> Float -> Point -> Bool 
julia n c r z0 = 
   let z = Complex z0.x z0.y f w = Complex (w.re ^ 2 - w.im ^ 2 + c.re) (2 * w.re * w.im + c.im) iter i zz = if i == n then True else if zz.re * zz.re + zz.im * zz.im > r then False else iter (i + 1) (f zz) in iter 0 z -- Color scheme function getColor : Float -> Color.Color getColor t = let r = (cos (t + 1 * pi / 3) + 1) / 2 g = (cos (t + 3 * pi / 3) + 1) / 2 b = (cos (t + 5 * pi / 3) + 1) / 2 in Color.rgb (r * 255) (g * 255) (b * 255) 

-- Julia set fractal drawing function 

juliaSet : Int -> Complex -> Float -> Point -> Form 
juliaSet n c r z0 = 
   let eps = 0.005 width = 400 height = 400 xRange = List.range (-2 - eps) (2 + eps) (4 / width) yRange = List.range (-2 - eps) (2 + eps) (4 / height) points = List.map (\y -> List.map (\x -> Point x y) xRange) yRange colors = List.map (\row -> List.map (\p -> if julia n c r p then getColor (sqrt (p.x ^ 2 + p.y ^ 2)) else Color.black ) row ) points forms = List.indexedMap (\i row -> List.indexedMap (\j c -> filled c (polygon [(Point (xRange.j) (yRange.i)), (Point (xRange.(j+1)) (yRange.i)), (Point (xRange.(j+1)) (yRange.(i+1))), (Point (xRange.j) (yRange.(i+1)))]) ) row ) colors in collage width height (concat (List.concat forms)) 
-- Program 

main : Program () () () 
main = Browser.element { init = \_ -> ((), juliaSet 100 { re = 0.3, im = 0.5 } 2 { x = -1.5, y = -1.5 }) 
, view = \_ -> juliaSet 100 { re = 0.3, im = 0.5 } 2 { x = -1.5, y = -1.5 }
, update = \_ _ -> ((), juliaSet 100 { re = 0.3, im = 0.5 } 2 { x = -1.5, y = -1.5 }) 
, subscriptions = \_ -> Sub.none } 

This version makes several improvements, including: - Uses more descriptive function and variable names. - Avoids using the Jeltor library for handling complex numbers, defining its own custom Complex type instead. - Uses better-defined types for arguments, such as Point and Complex. - Properly compose list operations using List.map, List.indexedMap, and so on, rather than manually iterating with for loops. - Uses explicit type annotations at key places for clarity. - Uses pattern matching in recursive functions instead of explicit indexing.

Put a line break somewhere in a long line and run elm-format. Then you’ll get more multiline stuff. Still too long a line somewhere? Another line break and run elm-format again. Too much multiline? Join some stuff up on a single line and re-run. Play around with it to get a feel for how it works. The thing here is that for some syntax, elm-format lets you decide some formatting by looking at where you have (or have not) placed newlines.

The whole text was a single line but it was quite easy to cut the lines as editor vim is so far syntax aware. The worst thing are now some very long anonymous functions which cannot be split (as I cut them, elm make proposed to use named functions.)
One problem remains, however. Its on the line 51 of the semifixed source - please look at the code.
Elm make does not accept indexing with (j+1) in this way: (Point (xRange.(j+1)) (yRange.i)),…
I don’t know how to fix it. Should (j+1) be replaced with a word e.g jnext?

let jnext = j + 1
in (Point (xRange.jnext) (yRange.i)),...

Detected problems in 1 module.
-- EXPECTING RECORD ACCESSOR ------------------------------------- chatreply.elm

I am trying to parse a record accessor here:

51|             points forms = List.indexedMap (\i row  -> List.indexedMap (\j c -> filled c (polygon [(Point (xRange.j) (yRange.i)), (Point (xRange.(j+1)) (yRange.i)), (Point (xRange.(j+1)) (yRange.(i+1))), (Point (xRange.j) (yRange.(i+1)))]) ) row ) colors 
                                                                                                                                                     ^
Something like .name or .price that accesses a value from a record.

Elm-format will be able to format Elm code for you, but will not give you partial results: it will tell you which parts are problematic, and you can iterate with that.

Note the AI might use syntax that is not available in Elm. You already found foo.(i+1). Depending on what is meant by that, you might replace that with List.Extra.getAt (i+1) foo or perhaps change the algorithm to use Array instead of List. if this random access is going to be happening a lot.

Not also that the content of main is nonsensical.

Yes, I found some examples more: AI code used nonexisting ‘Svg.group’ instead of ‘Svg.g’.
Further, in Haskell AI imported some nonexisting or missing libraries.

As a summary, I have got no working solution yet by AI after using it for about one week.
I have anyway learned that Svg is enough for me while doing code to fractals as webgl with mesh and shaders may be needed later.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.