[ prog / sol / mona ]

prog


Favorite Lisp dialect?

29 2024-03-10 09:15

Symta is very uniform and uses a single construct for what other languages have multiple functions or syntax constructs, or don't have an easy way to do at all.

L{:P} //keep elements matching pattern P
L{P=} //skip elements matching P
L{P=Body} //map elements matching P
L{~/3} //group list elements into lists of 3
L(:@_ P@) //check if list has an element matching P
L(:@_ -P&=0) //check if all list elements match P
L(/~ P@) //index of the element matching P
L(@_ ~<P@) //return the element matching P
L(/N@~) //drop first N elements
L(@~ /M) //drop last M elements
L(/N @~ /M) //drop first N elements and last M elements
L(~@) //first element of a list
L(_@~) //all list elements but the first.
L{/3=@Q.f} //change color order; same as L{R G B = B G R} 
L{/3=Q.z/3}  //convert to 8-bit grayscale; same as L{R G B = (R+G+B)/3}
L{/3&=Q.z/3..3} //convert to 24-bit grayscale
P{'/'='\\'} //'/usr/bin/bash' turns into '\usr\bin\bash'
P{@'usr'='usr/local'} //'/usr/bin/bash' turns into '/usr/local/bin/bash'
L{R G B = ~r.R ~g.G ~b.B} //split colors into separate planes
L{&~s+?} //sum elements of a list
L{:~a._,~b._} //split a list of pairs into two separate lists
L{:~a._,~b.(i~+)} //as above, but 2nd list gets integers from 0
Matrix: 1,2,3 4,5,6 7,8,9
Matrix{?.~~} // list diagonal elements of Matrix: 1,5,9
Matrix{&~s+?.~~} // sum diagonal elements of Matrix: 1+5+9
FilePath.lines{"[~~] [?]"^say} //print text file with each line prefixed by
                               //its number
S{~D.?+} //count character frequencies into the table bound to `~D`
         //return that table
L{?%2=~i+} //count the number of odd integers
L{~j+:?%2=~i+} //count number of even and odd integers
               //return as a list of two elements
L{~j.Q:?%2=~i.Q} //split even and odd integers into two separate lists
L{(max &~x ?)} //return the maximum element of a list
L{?%2=Q%} //return the first odd element in a list or No
L{?%2=~_=Q} //return the last odd element in a list or No
L{?%2=~~%} //return index of the first odd element in a list or No
L{?%2=~_=~~} //return index of the last odd element in a list or No
[a b c d e]{?,5+} //((a 5) (b 6) (c 7) (d 8) (e 9))
10{&1*2:} //(2 4 8 16 32 64 128 256 512 1024)
Xs{A,B = ~i.A ~j.B} //matrix transpose. Same as Xs.zip

Now, given a list `L: [1 1 1] [2 3 4] [5 6]`

say L{_ ~ _}  //says (1 3 (5 6))
say L{:_ ~ _}  //says (1 3)
say L{_ ~ _;~ _} //says (1 3 5)
say L{_ ~ ~} //((1 1) (3 4) (5 6))

Hope Symta got you interested. Want more examples?

For every character in text T, count number of occurencies, and output sorted by frequency

D!;S{&D.?+1};D.s|?1>??1

`D!` - declares a hash table D.
`S{@Body}` - apply @Body to every character of S
`D.?` - looks up character in a dictionary
`?` - unbound variable, turns entire formula into a lambda function
`&X+1`- add 1 to X and store back into X
`D.s` - sort dictionary. `.s` is a method call
`?1>??1` - sorting order for `.s`. `??` second argument for the lambda

Well, the above code is kinda verbose, so Symta allows for a shorthand

S{~D.?+}.s|?1>??1

`~D` - implcitly declared and returned hashtable
`+` - post increment operator, similar to `++` in C-based languages

If there are several ~Name variables, then they are returned as a list.

Here is more word processing. We all know the classic magic numbers, like 0xcafebabe and 0xA55ba11. But are there more? Given an English words dictionary `Ws`, find all the words representable in hex that way:

Ws{?{b=8;l=1;s=5;t=7}}.keep ?all^|'8157abcdef'.any ?

Compute huffman codes for a string

S "osteoclasts undergo apoptosis at the end of the bone resorption phase"
H heap; S{~D.?+}{|H.push@?f}  //count frequencies and add them to heap
(H.n-1){2{H.pop:}(A,B C,D=H.push A+C B,D):} //build tree
C [[]H.pop.1](:D,[L R] = @[0,@D L]^r @[1,@D R]^r; D,C=:C,D) //unroll tree
say C.s(?1.n<??1.n) //output sorted by code length
62


VIP:

do not edit these