recursive functions?

Pixilang programming language
Post Reply
User avatar
AutumnCheney
Posts: 503
Joined: Sun Dec 29, 2019 8:16 am
Location: tahlequah, ok, usa
Contact:

recursive functions?

Post by AutumnCheney »

i'm trying to use the sunvox curve generator script to generate chebyshev polynomials. i already have it set to generate an arbitrary number of curves, where the variable "$j" is the current curve number. i'm currently stuck on trying to define the function to generate the curves (which by nature must be recursive).

here's my code (pretend this is inside the curve_function):

Code: Select all

fn chebyshev ($x, $j) {
	if ($j == 0) { ret(1) }
	if ($j == 1) { ret($x) }
	else {$y = ret(((2 * $x) * chebyshev($x, ($j - 1))) - chebyshev($x, ($j - 2))) }
    }
    
$y = chebyshev($x, $j)

ret($y)
when i try to run this, pixilang says there's a syntax error on the line with the else statement

might be a typo, but i'm wondering whether this is even possible with pixilang

thank you in advance!
my website: https://acheney.xyz

it features my music, sunvox content, and social media links!
User avatar
NightRadio
Site Admin
Posts: 3941
Joined: Fri Jan 23, 2004 12:28 am
Location: Ekaterinburg. Russia
Contact:

Re: recursive functions?

Post by NightRadio »

Code: Select all

else { $y = ret(...
This is where the problem is :)
ret() operator doesn't return anything to the current code, it just exits the function.
Correct code will be something like

Code: Select all

else { ret(...
User avatar
AutumnCheney
Posts: 503
Joined: Sun Dec 29, 2019 8:16 am
Location: tahlequah, ok, usa
Contact:

Re: recursive functions?

Post by AutumnCheney »

NightRadio wrote: Wed Jul 27, 2022 11:32 am

Code: Select all

else { $y = ret(...
This is where the problem is :)
ret() operator doesn't return anything to the current code, it just exits the function.
Correct code will be something like

Code: Select all

else { ret(...
oh, that fixed it! thank you for bearing with me

the function works perfectly now!
my website: https://acheney.xyz

it features my music, sunvox content, and social media links!
User avatar
AutumnCheney
Posts: 503
Joined: Sun Dec 29, 2019 8:16 am
Location: tahlequah, ok, usa
Contact:

Re: recursive functions?

Post by AutumnCheney »

btw here's what i did with it: i made fourteen waveshaper curves corresponding to the chebyshev polynomials of the first kind, where n = 2 ... 15. the zip archive is attached to this post

when running a sine wave through the curve, it has the effect of pitching up the sine by one harmonic. sure, it aliases a lot, but it's still there. hq-spline mode for the waveshaper please @nightradio

i've also included the pixi script in the zip, so you can make your own. just change the "levels" variable to the number you want - 1

have fun! :)
Attachments
chebyshev.zip
(14.08 KiB) Downloaded 123 times
my website: https://acheney.xyz

it features my music, sunvox content, and social media links!
Post Reply