Beginner's Eratosthenes' Sieve in Lisp!

Hello!

I’ve been obsessing about the lisp language recently. I’ve been in the periphery with learning about Haskell and functional programming. I have actually kind of avoided learning lisp because of its “ugly” syntax at face-value, despite being raised by Emacs as my first (true) editor. I woke up one day and decided enough was enough, i’m gonna learn lisp and gain a deeper understanding of Emacs and also programming. And dear god was it so worth it.

Just today I coded this function for Eratosthenes’ sieve, and I had so much fun coding it! I like to go through Project Euler’s archived problems when starting off with a new language because it really forces me to interact with the code rather than passively reading a programming book (I’m reading Land of Lisp, it’s so unhinged I love it)


<span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;">defun </span><span style="font-weight:bold;color:#795da3;">range </span><span style="color:#323232;">(start end)
</span><span style="color:#323232;">  (</span><span style="font-weight:bold;color:#a71d5d;">if </span><span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;"><</span><span style="color:#323232;"> start end)
</span><span style="color:#323232;">      (</span><span style="color:#62a35c;">cons</span><span style="color:#323232;"> start (range (</span><span style="color:#0086b3;">1</span><span style="font-weight:bold;color:#a71d5d;">+</span><span style="color:#323232;"> start) end))))
</span><span style="color:#323232;">
</span><span style="font-style:italic;color:#969896;">;; Checks if d is a factor of n
</span><span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;">defun </span><span style="font-weight:bold;color:#795da3;">factorofp </span><span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;">d</span><span style="color:#323232;"> n)
</span><span style="color:#323232;">  (</span><span style="color:#62a35c;">zerop </span><span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;">rem</span><span style="color:#323232;"> n </span><span style="font-weight:bold;color:#a71d5d;">d</span><span style="color:#323232;">)))
</span><span style="color:#323232;">
</span><span style="font-style:italic;color:#969896;">;; Sieve in lisp??
</span><span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;">defun </span><span style="font-weight:bold;color:#795da3;">sieve </span><span style="color:#323232;">(n)
</span><span style="color:#323232;">  (</span><span style="font-weight:bold;color:#a71d5d;">let </span><span style="color:#323232;">((primes (range </span><span style="color:#0086b3;">2</span><span style="color:#323232;"> n))
</span><span style="color:#323232;">        (curprime </span><span style="color:#0086b3;">2</span><span style="color:#323232;">))
</span><span style="color:#323232;">    (</span><span style="font-weight:bold;color:#a71d5d;">maplist </span><span style="color:#323232;">(</span><span style="color:#62a35c;">lambda </span><span style="color:#323232;">(tail)
</span><span style="color:#323232;">               (</span><span style="color:#62a35c;">delete-if </span><span style="color:#323232;">(</span><span style="color:#62a35c;">lambda </span><span style="color:#323232;">(n)
</span><span style="color:#323232;">                            (factorofp curprime n))
</span><span style="color:#323232;">                          (</span><span style="color:#62a35c;">cdr</span><span style="color:#323232;"> tail))
</span><span style="color:#323232;">               (</span><span style="color:#62a35c;">setf</span><span style="color:#323232;"> curprime (</span><span style="color:#62a35c;">cadr</span><span style="color:#323232;"> tail)))
</span><span style="color:#323232;">             primes)
</span><span style="color:#323232;">    primes))
</span><span style="color:#323232;">
</span><span style="color:#323232;">
</span><span style="color:#323232;">CL-USER> (sieve </span><span style="color:#0086b3;">1000</span><span style="color:#323232;">)
</span><span style="color:#323232;">(</span><span style="color:#0086b3;">2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103
</span><span style="color:#323232;"> </span><span style="color:#0086b3;">107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199
</span><span style="color:#323232;"> </span><span style="color:#0086b3;">211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313
</span><span style="color:#323232;"> </span><span style="color:#0086b3;">317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433
</span><span style="color:#323232;"> </span><span style="color:#0086b3;">439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563
</span><span style="color:#323232;"> </span><span style="color:#0086b3;">569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673
</span><span style="color:#323232;"> </span><span style="color:#0086b3;">677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811
</span><span style="color:#323232;"> </span><span style="color:#0086b3;">821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941
</span><span style="color:#323232;"> </span><span style="color:#0086b3;">947 953 967 971 977 983 991 997</span><span style="color:#323232;">)
</span>

I love lisp because it is at its core a functional programming language, but (as i do in my sieve function with the outermost lambda) i can specify localized points where I define, use, and mutate state. It gives me the best of both worlds, functional and imperative.

Lisp has made me kinda like coding again. Every function feels like writing poetry, especially with the indentations. People say our parentheses are ugly but they’re wrong and they’re the ugly ones.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • lisp@lemmy.ml
  • DreamBathrooms
  • ngwrru68w68
  • modclub
  • magazineikmin
  • thenastyranch
  • rosin
  • khanakhh
  • InstantRegret
  • Youngstown
  • slotface
  • Durango
  • kavyap
  • mdbf
  • GTA5RPClips
  • JUstTest
  • ethstaker
  • normalnudes
  • tester
  • osvaldo12
  • everett
  • cubers
  • tacticalgear
  • anitta
  • Leos
  • provamag3
  • cisconetworking
  • megavids
  • lostlight
  • All magazines