Compare length of two strings: Difference between revisions

m
(adding lambdatalk contribution)
m (→‎{{header|Lambdatalk}}: a minor change)
Line 1,103:
 
=={{header|Lambdatalk}}==
UsingLambdatalk onlycomes 3 lambdatalkwith primitives working on pairswords, [consW.equal?, carW.length, cdr...], andon 1 lambdatalk primitive testing the equality of 2 wordssentences, W[S.equalempty?, weS.first, defineS.rest, the L.sort..] functionand waitingpairs, for[P.new, aP.left, predicateP.right, function and a list...].
 
Using 2 lambdatalkthese primitives working on words, [W.equal?, W.length], and 3 lambdatalk primtives working on sentences, [S.empty?, S.first, S.rest], we define 2 helper functions, [L.new, L.disp], to build and display a list according to a chosen format.
<lang Scheme>
{def L.sort
{lambda {:filter :l}
{if {W.equal? :l nil}
then nil
else {L.insert {car :l} :filter
{L.sort :filter {cdr :l}}}}}}
-> L.sort
 
{def L.insert
{lambda {:x :filter :l}
{if {W.equal? :l nil}
then {cons :x nil}
else {if {:filter :x {car :l}}
then {cons :x :l}
else {cons {car :l}
{L.insert :x :filter {cdr :l}}}}}}}
-> L.insert
</lang>
 
Using 2 lambdatalk primitives working on words, [W.equal?, W.length], and 3 lambdatalk primtives working on sentences, [S.empty?, S.first, S.rest], we define 2 helper functions, [L.new, L.disp], to build and display a list according to a chosen format.
<lang Scheme>
{def L.new
{lambda {:s}
{if {S.empty? {S.rest :s}}
then {consP.new {S.first :s} nil}
else {consP.new {S.first :s} {L.new {S.rest :s}}}}}}
-> L.new
 
{def L.disp
Line 1,138 ⟶ 1,118:
{if {W.equal? :l nil}
then
else {br} {W.length {carP.left :l}} : {carP.left :l}
{L.disp {cdrP.right :l}}}}}
-> L.disp
</lang>
Line 1,157 ⟶ 1,137:
</lang>
 
Then we define the L.sort function waiting for a predicate function and a list.
Defining a predicate function (which could be anonymous) testing the length of 2 words
 
<lang Scheme>
{def L.sort
{lambda {:filter :l}
{if {W.equal? :l nil}
then nil
else {L.insert {carP.left :l} :filter
{L.sort :filter {cdrP.right :l}}}}}}
-> L.sort
 
{def L.insert
{lambda {:x :filter :l}
{if {W.equal? :l nil}
then {consP.new :x nil}
else {if {:filter :x {carP.left :l}}
then {consP.new :x :l}
else {consP.new {carP.left :l}
{L.insert :x :filter {cdrP.right :l}}}}}}}
-> L.insert
</lang>
 
DefiningUsing athe following predicate function (which could be anonymous) testing the length of 2 words
 
<lang Scheme>