[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

# Aw: Re: [sc-users] recursion

```> Gesendet: Sonntag, 29. Dezember 2019 um 06:00 Uhr
> Von: rohrhuber@xxxxxxxxxxxxxx
> An: sc-users@xxxxxxxxxxxxxxxx
> Betreff: Re: [sc-users] recursion
>
> and it it’s OK to have the array backwards, you can do it with a pure function:
>
> (
> ~collatz_R = { arg num, col, limit = 1000;
> 	if((num != 1) and: { limit > 0 }) {
> 		~collatz_R.(
> 			if(num.even) { num.div(2) } { num * 3 + 1 },
> 			col,
> 			limit - 1
> 		)
> 	}.add(num)
> };
> )
>

Haha, I love you people, welcome to the SC pluriverse !

BTW sometimes it might be useful to write code without round brackets as it can significantly reduce the probability of bracket confusion [or it can help with an incomplete keyboard]

This version also works without semicolons:

~collatz_R = { |num, col, limit = 1000|
num != 1 and: { limit > 0 } if: {
~collatz_R valueArray: [
num.even.if { num div: 2 } { num * 3 + 1 },
col,
limit - 1
]
} add: num
}

~collatz_R value: 23

-> [ 1, 2, 4, 8, 16, 5, 10, 20, 40, 80, 160, 53, 106, 35, 70, 23 ]

Regards

Daniel

-----------------------------
http://daniel-mayer.at
-----------------------------

_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: https://listarc.bham.ac.uk/marchives/sc-users/
search: https://listarc.bham.ac.uk/lists/sc-users/search/

```