lambdaway
::
WSAP
6
|
list
|
login
|
load
|
|
_h1 WSA(P) | [[WSAPL]] | [[meta10]] _p Lambdatalk has a fondamental set of three primitives working on {b Words, Sentences & Arrays}. {b Pairs} are added as primitives to build {b Lists}, which are special compositions of Arrays built on demand as user defined functions. {b Transtypage} is done via six user defined functions which should be promoted as primitives. _h2 1) primitives _h3 1.1) words {prewrap '{W.lib} -> {W.lib} '{def W hello} -> {def W hello} '{W} -> {W} '{W.equal? hello world} -> {W.equal? hello world} '{W.equal? hello hello} -> {W.equal? hello hello} '{W.empty? {W}} -> {W.empty? {W}} '{W.length {W}} -> {W.length {W}} '{W.get 1 {W}} -> {W.get 1 {W}} '{W.first {W}} -> {W.first {W}} '{W.rest {W}} -> {W.rest {W}} '{W.last {W}} -> {W.last {W}} '{W.slice 1 4 {W}} -> {W.slice 1 4 {W}} '{W.reverse {W}} -> {W.reverse {W}} '{W.sort < 53241} -> {W.sort < 53241} '{W.sort > 53241} -> {W.sort > 53241} '{W.sort before hello} -> {W.sort before hello} '{W.sort after hello} -> {W.sort after hello} } _p A small example: {pre '{def W.capitalizeInRed {lambda {:w} {span {@ style="text-transform:uppercase;color:red;"} {W.first :w}}{W.rest :w}}} -> {def W.capitalizeInRed {lambda {:w} {span {@ style="text-transform:uppercase;color:red;"} {W.first :w}}{W.rest :w}}} '{W.capitalizeInRed world} -> {W.capitalizeInRed world} } _p See also [[addstrbin]]. _h3 1.2) sentences {prewrap '{S.lib} -> {S.lib} '{def S a b c d e f g} -> {def S a b c d e f g} '{S} -> {S} '{S.empty? {S}} -> {S.empty? {S}} '{S.length {S}} -> {S.length {S}} '{S.first {S}} -> {S.first {S}} '{S.rest {S}} -> {S.rest {S}} '{S.last {S}} -> {S.last {S}} '{S.get 1 {S}} -> {S.get 1 {S}} '{S.slice 1 5 {S}} -> {S.slice 1 5 {S}} '{S.serie 0 10 2} -> {S.serie 0 10 2} '{S.map {lambda {:x} {* :x :x}} {S.serie 1 9}} -> {S.map {lambda {:x} {* :x :x}} {S.serie 1 9}} '{S.reduce * {S.serie 1 6}} -> {S.reduce * {S.serie 1 6}} '{S.reduce {{lambda {:a :b} {* :a :b}} 1} {S.serie 1 6}} -> {S.reduce {{lambda {:a :b} {* :a :b}} 1} {S.serie 1 6}} '{S.replace x by brave in hello x new world} -> {S.replace x by brave in hello x new world} '{S.reverse hello brave new world} -> {S.reverse hello brave new world} '{S.sort < 4 3 5 2 1} -> {S.sort < 4 3 5 2 1} '{S.sort > 4 3 5 2 1} -> {S.sort > 4 3 5 2 1} '{S.sort before hello brave new world} -> {S.sort before hello brave new world} '{S.sort after hello brave new world} -> {S.sort after hello brave new world} } _p A small example {pre '{def S.capitalizeInRed {lambda {:s} {S.map W.capitalizeInRed :s}}} -> {def S.capitalizeInRed {lambda {:s} {S.map W.capitalizeInRed :s}}} '{S.capitalizeInRed hello brave new world} -> {S.capitalizeInRed hello brave new world} } _h3 1.3) arrays {prewrap '{A.lib} -> {A.lib} '{def A {A.new 1 2 3 4 5 6}} -> {def A {A.new 1 2 3 4 5 6}} '{A} -> {A} '{def B {A.new 7 8 9}} -> {def B {A.new 7 8 9}} '{B} -> {B} the following functions don't modify arrays '{A.join {A.new 1 2 3 4 5 6}} -> {A.join {A.new 1 2 3 4 5 6}} '{A.split 123456} -> {A.split 123456} '{A.equal? {A} {A}} -> {A.equal? {A} {A}} '{A.equal? {A} {B}} -> {A.equal? {A} {B}} '{A.array? {A}} -> {A.array? {A}} '{A.array? hello} -> {A.array? hello} '{A.null? {A}} -> {A.null? {A}} '{A.null?} -> {A.null?} '{A.empty? {A}} -> {A.empty? {A}} '{A.empty? {A.new}} -> {A.empty? {A.new}} '{A.in? 3 {A}} -> {A.in? 3 {A}} '{A.in? 9 {A}} -> {A.in? 9 {A}} '{A.length {A}} -> {A.length {A}} '{A.get 1 {A}} -> {A.get 1 {A}} '{A.first {A}} -> {A.first {A}} '{A.rest {A}} -> {A.rest {A}} '{A.last {A}} -> {A.last {A}} '{A.slice 1 5 {A}} -> {A.slice 1 5 {A}} '{A.duplicate {A}} -> {A.duplicate {A}} '{A.reverse {A}} -> {A.reverse {A}} '{A.concat {A} {B}} -> {A.concat {A} {B}} '{A.map b {A}} -> {A.map b {A}} '{A.map {lambda {:i} {* :i :i}} {A}} -> {A.map {lambda {:i} {* :i :i}} {A}} the following functions return the same array modified '{A.set! 1 _ {A.new 1 2 3 4 5}} -> {A.set! 1 _ {A.new 1 2 3 4 5}} '{A.addlast! _ {A.new 1 2 3 4 5}} -> {A.addlast! _ {A.new 1 2 3 4 5}} '{A.addfirst! _ {A.new 1 2 3 4 5}} -> {A.addfirst! _ {A.new 1 2 3 4 5}} '{A.subfirst! {A.new 1 2 3 4 5}} -> {A.subfirst! {A.new 1 2 3 4 5}} '{A.sublast! {A.new 1 2 3 4 5}} -> {A.sublast! {A.new 1 2 3 4 5}} '{A.reverse! {A.new 1 2 3 4 5}} -> {A.reverse! {A.new 1 2 3 4 5}} '{A.sort! > {A.new 3 1 5 4 2}} -> {A.sort! > {A.new 3 1 5 4 2}} '{A.sort! < {A.new 3 1 5 4 2}} -> {A.sort! < {A.new 3 1 5 4 2}} '{A.sort! before {A.new b c a z x}} -> {A.sort! before {A.new b c a z x}} '{A.sort! after {A.new b c a z x}} -> {A.sort! after {A.new b c a z x}} } _p The lengthy {b '{A.new ...}} can be replaced by a shorter syntax, {b #{span}(...)}, using a macro {pre '{macro #\(([^\)]*)\) to {A.new €1}} {macro #\(([^\)]*)\) to {A.new €1}} } _p So, instead of {pre '{A.new 1 2 3} -> {A.new 1 2 3} '{A.new} -> {A.new} } _p we can write {pre #{span}(1 2 3) -> #(1 2 3) #{span}() -> #() } _p Being mutable arrays can be used as a sandbox for iterative processes. For instance {b fib & fac} _h3 fib {pre 1: [0,1] 2: [1,1] 3: [1,2] 4: [2,3] 5: [3,5] 6: [5,8] 7: [8,13] 8: [13,21] 9: [21,34] 10: [34,55] '{let { {:fib {lambda {:a :i} // local function {A.set! 1 {long_add {A.get 0 :a} {A.get 1 :a}} {A.set! 0 {A.get 1 :a} :a}} }} } {S.last {S.map {:fib {A.new 0 1}} // initialization {S.serie 2 10}} } // loop } -> {let { {:fib {lambda {:a :i} {A.set! 1 {long_add {A.get 0 :a} {A.get 1 :a}} {A.set! 0 {A.get 1 :a} :a}} }} } {S.last {S.map {:fib {A.new 0 1}} {S.serie 2 10}} } } } _p or, using an anonymous function partially initialized {prewrap '{A.last {S.last {S.map {{lambda {:a :i} {A.set! 1 {long_add {A.get 0 :a} {A.get 1 :a}} {A.set! 0 {A.get 1 :a} :a}} } {A.new 0 1}} {S.serie 2 1000}} }} -> {A.last {S.last {S.map {{lambda {:a :i} {A.set! 1 {long_add {A.get 0 :a} {A.get 1 :a}} {A.set! 0 {A.get 1 :a} :a}} } {A.new 0 1}} {S.serie 2 1000}} }} } _h3 fac {prewrap '{A.last {S.last {S.map {{lambda {:a :i} {A.set! 0 {long_mult :i {A.get 0 :a}} :a} } {A.new 1}} {S.serie 1 100}} }} -> {A.last {S.last {S.map {{lambda {:a :i} {A.set! 0 {long_mult :i {A.get 0 :a}} :a} } {A.new 1}} {S.serie 1 100}} }} } _h3 1.4) pairs _p Pairs are are special compositions of Arrays and are added as primitives to build easily Lists, on demand as user defined functions. {prewrap '{P.lib} -> PAIR: [7] [P.new|cons, P.left|car, P.right|cdr, P.pair?] '{def P {P.new hello world}} -> {def P {P.new hello world}} '{P} -> {P} '{P.pair? {P.new hello world}} -> {P.pair? {P.new hello world}} '{P.pair? hello} -> {P.pair? hello} '{P.left {P.new hello world}} -> {P.left {P.new hello world}} '{P.right {P.new hello world}} -> {P.right {P.new hello world}} } _p As a first example, {pre '{def P {P.new hello world}} -> {def P {P.new hello world}} '{P.left {P}} -> {P.left {P}} '{P.right {P}} -> {P.right {P}} } _p Let's build a list and related functions. Lists are nested pairs ending with {b nil}. {pre '{def L.new {lambda {:s} {if {W.equal? {S.rest :s} nil} then {P.new {S.first :s} nil} else {P.new {S.first :s} {L.new {S.rest :s}}}}}} -> {def L.new {def L.new.r {lambda {:s} {if {W.equal? {S.rest :s} nil} then {P.new {S.first :s} nil} else {P.new {S.first :s} {L.new.r {S.rest :s}}}}}} {lambda {:s} {L.new.r :s nil}}} '{def L {L.new a b c d}} -> {def L {L.new a b c d}} = {L} '{def P.nil? {lambda {:l} {W.equal? :l nil}}} -> {def P.nil? {lambda {:l} {W.equal? :l nil}}} '{def L.disp {lambda {:l} {if {P.nil? :l} then else {P.left :l} {L.disp {P.right :l}}}}} -> {def L.disp {lambda {:l} {if {P.nil? :l} then else {P.left :l} {L.disp {P.right :l}}}}} '{L.disp {L}} -> {L.disp {L}} '{def L.length {lambda {:l} {if {P.nil? :l} then 0 else {+ 1 {L.length {P.right :l}}}}}} -> {def L.length {lambda {:l} {if {P.nil? :l} then 0 else {+ 1 {L.length {P.right :l}}}}}} '{L.length {L}} -> {L.length {L}} '{def L.reverse {def L.reverse.r {lambda {:l1 :l2} {if {P.nil? :l1} then :l2 else {L.reverse.r {P.right :l1} {P.new {P.left :l1} :l2}}}}} {lambda {:l} {L.reverse.r :l nil}} } -> {def L.reverse {def L.reverse.r {lambda {:l1 :l2} {if {P.nil? :l1} then :l2 else {L.reverse.r {P.right :l1} {P.new {P.left :l1} :l2}}}}} {lambda {:l} {L.reverse.r :l nil}} } '{L.reverse {L}} -> {L.reverse {L}} '{def L.concat {def L.concat.r {lambda {:l1 :l2} {if {P.nil? :l1} then :l2 else {L.concat.r {P.right :l1} {P.new {P.left :l1} :l2}}}}} {lambda {:l1 :l2} {L.concat.r {L.reverse :l1} :l2}}} -> {def L.concat {def L.concat.r {lambda {:l1 :l2} {if {P.nil? :l1} then :l2 else {L.concat.r {P.right :l1} {P.new {P.left :l1} :l2}}}}} {lambda {:l1 :l2} {L.concat.r {L.reverse :l1} :l2}}} '{L.disp {L.concat {L} {L.reverse {L}}}} -> {L.disp {L.concat {L} {L.reverse {L}}}} and so on... } _h2 2) transtypage _p Transtypage is done via six user defined functions, which could be promoted as primitives. _h3 2.1) words {prewrap '{def W.toS {lambda {:w} {if {W.empty? {W.rest :w}} then {W.first :w} else {W.first :w} {W.toS {W.rest :w}}}}} -> {def W.toS {lambda {:w} {if {W.empty? {W.rest :w}} then {W.first :w} else {W.first :w} {W.toS {W.rest :w}}}}} '{def W.toA {def W.toA.r {lambda {:w :a} {if {W.empty? {W.rest :w}} then {A.addlast! {W.first :w} :a} else {W.toA.r {W.rest :w} {A.addlast! {W.first :w} :a}} }}} {lambda {:w} {W.toA.r :w {A.new}}}} -> {def W.toA {def W.toA.r {lambda {:w :a} {if {W.empty? {W.rest :w}} then {A.addlast! {W.first :w} :a} else {W.toA.r {W.rest :w} {A.addlast! {W.first :w} :a}} }}} {lambda {:w} {W.toA.r :w {A.new}}}} '{def W' abcd} -> {def W' abcd} '{W'} -> {W'} '{W.toS {W'}} -> {W.toS {W'}} '{W.toA {W'}} -> {W.toA {W'}} } _h3 2.2) sentences {pre '{def S.toW {lambda {:s} {if {S.empty? {S.rest :s}} then {S.first :s} else {S.first :s}{S.toW {S.rest :s}}}}} -> {def S.toW {lambda {:s} {if {S.empty? {S.rest :s}} then {S.first :s} else {S.first :s}{S.toW {S.rest :s}}}}} '{def S.toA {def S.toA.r {lambda {:a :s} {if {S.empty? {S.rest :s}} then {A.addlast! {S.first :s} :a} else {S.toA.r {A.addlast! {S.first :s} :a} {S.rest :s}} }}} {lambda {:s} {S.toA.r {A.new} :s}}} -> {def S.toA {def S.toA.r {lambda {:a :s} {if {S.empty? {S.rest :s}} then {A.addlast! {S.first :s} :a} else {S.toA.r {A.addlast! {S.first :s} :a} {S.rest :s}} }}} {lambda {:s} {S.toA.r {A.new} :s}}} '{def S' a b c d} -> {def S' a b c d} '{S.toW {S'}} -> {S.toW {S'}} '{S'} -> {S'} '{S.toA {S'}} -> {S.toA {S'}} } _h3 2.3) arrays {pre '{def A.toW {lambda {:a} {if {A.empty? :a} then else {A.first :a}{A.toW {A.rest :a}}}}} -> {def A.toW {lambda {:a} {if {A.empty? :a} then else {A.first :a}{A.toW {A.rest :a}}}}} '{def A.toS {lambda {:a} {if {A.empty? :a} then else {A.first :a} {A.toS {A.rest :a}}}}} -> {def A.toS {lambda {:a} {if {A.empty? :a} then else {A.first :a} {A.toS {A.rest :a}}}}} '{def A' {A.new a b c d}} -> {def A' {A.new a b c d}} '{A.toW {A'}} -> {A.toW {A'}} '{A.toS {A'}} -> {A.toS {A'}} '{A'} -> {A'} } _h2 flatten a nested array {prewrap '{def A.flatten {def A.flatten.r {lambda {:a} {if {A.empty? :a} then else {let { {:b {A.first :a}} } {if {A.array? :b} then {A.flatten.r :b} else :b} } {A.flatten.r {A.rest :a}} }}} {lambda {:a} {A.new {A.flatten.r :a}}}} -> {def A.flatten {def A.flatten.r {lambda {:a} {if {A.empty? :a} then else {let { {:a {A.first :a}} } {if {A.array? :a} then {A.flatten.r :a} else :a} } {A.flatten.r {A.rest :a}} }}} {lambda {:a} {A.new {A.flatten.r :a}}}} '{def nested_array {A.new 1 {A.new 2 3} {A.new a {A.new b c} d} {A.new 5 6} 7 }} -> {def nested_array {A.new 1 {A.new 2 3} {A.new a {A.new b c} d} {A.new 5 6} 7 }} '{nested_array} -> {nested_array} '{A.flatten {nested_array}} -> {A.flatten {nested_array}} '{def list_array {A.new 1 {A.new 2 {A.new 3 {A.new 4}}}}} -> {def list_array {A.new 1 {A.new 2 {A.new 3 {A.new 4}}}}} '{list_array} -> {list_array} '{A.flatten {list_array}} -> {A.flatten {list_array}} [[http://www.rosettacode.org/wiki/Flatten_a_list| http://www.rosettacode.org/wiki/Flatten_a_list]] [[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []] '{def ros {A.new {A.new 1} 2 {A.new {A.new 3 4} 5} {A.new {A.new {A.new }}} {A.new {A.new {A.new 6}}} 7 8 {A.new} }} -> {def ros {A.new {A.new 1} 2 {A.new {A.new 3 4} 5} {A.new {A.new {A.new }}} {A.new {A.new {A.new 6}}} 7 8 {A.new} }} '{A.flatten {ros}} -> {A.flatten {ros}} }
lambdaway v.20211111