Skip to content

Latest commit

 

History

History
77 lines (64 loc) · 7.7 KB

README.md

File metadata and controls

77 lines (64 loc) · 7.7 KB

Stutter

Implementation for the Stutter Programming Language

Stutter is a functional, lisp-like language

To run, you must first put the stdlib in the correct directory

mkdir $HOME/.stutter
cp stdlib.lisp $HOME/.stutter/stdlib.lisp

An example of Stutter syntax can be seen below with the standard library implementation of quicksort

(def quicksort
  (lambda (mylist)
    (if (empty mylist)
      (list)
      (cat (quicksort (filter (lambda (x) (< x (head mylist))) (tail mylist)))
           (list (head mylist))
           (quicksort (filter (lambda (x) (>= x (head mylist)))
                              (tail mylist)))))))

λ (quicksort (list 2 3 5 4 3 2 1 100 -1))
(-1 1 2 2 3 3 4 5 100)

Or below is how one could implement the collatz conjecture (https://en.wikipedia.org/wiki/Collatz\_conjecture)

(def collatz
  (lambda (x)
    (if (= 1 x)
      (list 1)
      (if (= 0 (mod x 2))
        (cat (list x) (collatz (/ x 2)))
        (cat (list x) (collatz (+ 1 (* 3 x))))))))

λ (collatz 100)
(100 50 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1)

One can even do differential calculus with Stutter

(def deriv
  (lambda (f x)
    (/
      (- (f (+ x 0.001))
          (f x))
      0.001)))

λ (deriv (lambda (x) (pow x 3.0)) 5.0) ;;; derivative of x^3 where x = 5
75.01500100002545

Stutter represents integers with arbitrary precision, so you can do HUGE calculations

λ (- (factorial 2000) (factorial 1999) (factorial 128) (factorial 7))
331461695490440792455480568388603587626197664950172750437464946934185058866648021989073094244302725959274651764755221192203455775006586149233320940209580517061181241526885858634814693945584712532465347273382541335488304155271668608949466816264580555365087094878304924251439140817784328392742088226479634027471256929301081700950330014376584759463665916374040827531949211905414223625652425843825795606023001471322887232167520478877064976205537666958319857125971426146559170294249018076170797534708428548636356197935608523803026512061132282545728587588658503910630047335416888859996360818136429091897214387430155566424516457770186823464672271916718551930468773355390013684695634876439152148804331670510174977048063848459868299339387479558001467403075697549656515868939940000837312707303086633392821556899461459534956638660740402594970275861031343111704252567582835022560402226070840997413613692681276048285594096210199255794259517517973171809150740091067628584426794188015373775192556544771320660177395971064631638981272528129090039777695911898397112195958065592655499838412193127913258789218009234603536214747614394782159407809576382035957035838394935333284158137332921390249322655274682085806492979162603234520182172327573884561933571901274285171082777200263555450626647258604519476020374453061012675067004481865748509357847576645916846007973058446463099158191126375190870673756736486990983911988289434575634962315330404588607097070180213810343828830567664604025903181210782278629237295950348445286043884219093968218101050820239848886167605265427798618310596169617370943621883371357139259938758280073779154485633453668692422586851883609148194419504690043770348404046191571167136868202344706141131321395492035034333390506831450226660756321832668303552843463931936044115705421131688001602113688856098912915714264864201555181330741443069528336287816597529554663722031289822629072365682526934380274007573589287114392334950848693966855511774419899395922828778857067864161848285607777690254499018753762820056092689014222183609469410394628995195383186050087572849736998382533477639726137467532159759680450506678138746011344976758222536829594090430755830926476486137676293691450576290777685459917832633070537420088725400818675003033008610509762688326197647052894926111194586583119865918331658796232584066658241962537920101714216840120868965516161224740987612038114335695092862633991998530771798438478375313901394698277885658039278390185510589107567182908005617515697219404789764583883937893527132524480617605724019807829234076265960746955472375054554295529619960553795065410854843444972837812755385640526577736815075414293817315571948111771964483276549931167178317522130023165192352945001727495910405041311047963374127687870488549383154659084631872842378338793805673308080686449432447333770669671147439174245121190706720144751332143807959941060522094611007755072075863518372029310015047347383788457562493346402792841253051162049515498865672609999505920695181742362772424639478563026236827435907051828650569055069166466851456847850653826617601514038584917176563690583672697778541766126787405649820002781308925362526863199838112171837384496941891817447955240610031779228266220202132177202654258423687985055980332993872596914032946565332244493230033099667064228537195145628087396243961892117493816633951457568506248194286773947135214685015373460321583903222248930602145055179991235197318122342945816746502942003849697541064887297592986858583300250049759771959982137553570932703249107680307834291218248446559787439060048870598049325837932895283860168459154412919958269064381514595771450396901014203759362886232513687943975991772411448472912230790645036185120210168707696062638279957892485865977728772356045879440605385921983958346594758589158151660933965409793205825749789263145106241344175358893598473973837422296888913491417179995477105382216570038237507517460834224091260838377903355776067009268098751060919468536465610861260007365356451836780677391253781270976523546674771902781156006784568447827905001474789767367094608218963550676359941443486856401387714598573392193863663050182477495206951075676633135551820777247185648313719322426765185016154231423495229868220089806797306368016846088123067694049308160462116195717182358005399943505931311403089398010743553844847377808656393851830550211622201598864875233505692628166271222336958296693941541058908317658990725524850593202073560139534142787272995615232837185170619447274573814404804689459914470250116891917213619432075165453166340216504804207117699642071135884731003664285490596987660054926257092659227763954454754390804291823327729598729529888907254617981968902221909281984804245263651830639779379771703221174100035247181400611259773740062902516469862122016051254618451014959298797482148619935038585151253974073923809379643272006318932533583436153893324997634837515878155305320965631198913142856575233441266996295480100712887634270707605042792554127041728113221890203698662828552388846638848718897073455175836156046396990243718008685346440268226655598986926788317390399932297368624733572161554150169696386587605597882927593859388346028262696913986633973891467075442536826006746755188283549635788874008379560741327420178794613706090438922201117643751059038064196270498476501279792720273319353384959999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999614379517637419578264322934076536359382506890409776409721171596723626597424834456439313831411492638465969948166941083652407827067737501142233885044754960642239965355290720752307504414719999999999999999999999999994960

Stutter is interactive, and includes a REPL for experimentation

λ (filter isprime (range 0 100))
(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)

With Stutter being a functional language, it is well suited for solving mathematical problems, such as those found on https://projecteuler.net/

λ (sum (filter even (filter (lambda (x) (< x 4000000)) (fibonacci 1000))))
4613732

Try calculating the golden ratio using the fibonacci sequence!

λ (last (map (lambda (x) (/ (head x) (last x))) (zip (tail (fibonacci 100)) (fibonacci 100))))
1.618033988749895