blog/2025-07-31/the-loop-and-more-loops/ #256
Replies: 7 comments 3 replies
-
|
Hey ingy, I'm very fresh to Yamlscript and clojure.. I have tried for a few hours to write a simple yamlscript isPrime function, and am having no luck (similarly, none of the AI chat bots are having any luck either!). How would you do it? I am coming from more of a Javascript / Typescript / Go background. Trying to assign / reassign variables outside of loops, etc? The whole thing has got me pretty messed up :/ I looked over some of the rosetta stuff too, but still am just missing something |
Beta Was this translation helpful? Give feedback.
-
|
Hi @pwdrysdale ! Apologies for not seeing this sooner! Here's a quick example of defining and calling a !YS-v0
defn main(*nums):
each num nums: !:say
if prime?(num): +
"$num is prime"
"$num is not prime"
defn prime?(n):
(n > 1) &&
\(zero?(n % _))
.not-any?(range(2 n))and run it: There's a lot to discuss here. How about instead of me trying to tell you the finer points here, let's turn this into a dialog. Looking forward to your replies... |
Beta Was this translation helpful? Give feedback.
-
|
WRT to functional (FP) versus imperative programming, it's a bit of a mind switch but not a huge one. Say you have a variable called You never changed a mapping here. You just created a new one and then re-bound Another thing to keep in mind is that you can't return early from a function. Hope this sparks more questions. |
Beta Was this translation helpful? Give feedback.
-
|
Hey Ingy, thanks for writing back, and sorry, just getting back to this now. The math is not the problem - Math was my undergraduate degree. I can see that you're simply taking the modulo for all numbers less that the number and down to 2, and checking that there is remainder - a pretty standard programming way of checking for whether or not a number is prime. Where I get lost is really the syntax and semantics - I am primarily a typescript dev, which is not semantic. I put some of your code into AI, so we'll see how we go for understanding: It is mostly here where I got caught / blocked. So it looks like The I assume then that the The rest of the top comment (e.g. the main function) seems fine. I am fairly used to dealing with immutability thanks to React. The thing about that second comment is just to do with syntax, again. The You are right about it being a mind shift! |
Beta Was this translation helpful? Give feedback.
-
|
Here's a variation of vendethiel's solution that's pretty nice. We can get rid of the check for greater than or equal to 2 by using the range or 2 to infinity. We can run this program to find the 10th prime like this: The 10000th prime takes a while: A quick improvement is to only check up to the square root of the number: and then: I'm sure you can think of several optimizations but I leave them to you. |
Beta Was this translation helpful? Give feedback.
-
|
@pwdrysdale about looping...
Getting good at thinking about list manipulations is a key to getting good at functional programming. Note: |
Beta Was this translation helpful? Give feedback.
-
|
@pwdrysdale (and anyone else). Join us on our Discord chat: https://discord.gg/GYuCq6Au |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
blog/2025-07-31/the-loop-and-more-loops/
Program in YAML — Code is Data
https://yamlscript.org/blog/2025-07-31/the-loop-and-more-loops/
Beta Was this translation helpful? Give feedback.
All reactions