Amb: Difference between revisions

372 bytes removed ,  1 year ago
→‎{{header|J}}: re-implement task
(→‎{{header|J}}: re-implement task)
Line 1,898:
 
=={{header|J}}==
Here, we interpret the specification such that ambassert picks an arbitrary valid example. In other words, it discards all unnecessary ambiguity (it arbitrarily picks a "single future" from among the valid "potential futures").
<syntaxhighlight lang="j"> amb=. ([ , ' ' , ])&>/&.>@:((({:@:[ = {.@:])&>/&> # ])@:,@:({@(,&<)))
 
>@(amb&.>/) ('the';'that';'a');('frog';'elephant';'thing');('walked';'treaded';'grows');(<'slowly';'quickly')
Since this task specifies an O(n^m) complexity problem, we'll also assume that the user knows to limit its use to small examples.
+-----------------------+
 
|that thing grows slowly|
Implementation:
+-----------------------+</syntaxhighlight>
 
amb is a dyadic verb:
<syntaxhighlight lang=J>ambsel=: <^:99@,
<syntaxhighlight lang="j"> ('the';'that';'a') amb ('frog';'elephant';'thing') amb ('walked';'treaded';'grows') amb ('slowly';'quickly')
 
+-----------------------+
ambassert=: {{
|that thing grows slowly|
anames=. (#~ 99<:{{L.do y}}@>) (#~ 0=nc)/:~~.;:m
+-----------------------+</syntaxhighlight>
avalues=. {{>^:98 do y}}every anames
A structured derivation of amb follows:
limit=. */alimits=. #@> avalues
<syntaxhighlight lang="j"> NB. Dynamic programming method...
j=. 0 while. do.
ainds=. alimits#:j
(anames)=. test=. ainds {each avalues
try. do m
anames {{(x)=:y}}&> test
break.
catch. end.
if. limit<:j=.j+1 do. assert.'no valid solution' end.
end.
EMPTY
+-----------------------+}}</syntaxhighlight>
 
Task example:
 
<syntaxhighlight lang="j"J> NB.s1=: Dynamicambsel;:'the programmingthat method...a'
s2=: ambsel;:'frog elephant thing'
s3=: ambsel;:'walked treaded grows'
s4=: ambsel;:'slowly quickly'
edgematch=: {{ x,y assert. ({:;x)={.;y }}
o=. @: NB. Composing verbs
success=. {:o[ = {.o] NB. Is the last letter of the left word equal to the first of the right?
join=. [ , ' ' , ] NB. Joining the left and right words
cp=. {@(,&<) NB. Cartesian product
{{)n edgematch/ s1,s2,s3,s4 }} ambassert
amb=. join&>/&.> o ((success&>/ &> # ]) o , o cp)f.
amb NB. Showing the point-free code...
echo s1,s2,s3,s4
([ , ' ' , ])&>/&.>@:((({:@:[ = {.@:])&>/&> # ])@:,@:({@(,&<)))</syntaxhighlight>
┌────┬─────┬─────┬──────┐
 
│that│thing│grows│slowly│
Note that <code>amb</code> here is roughly equivalent to the <code>Ambassert</code> in the task description, and that the corresponding <code>Ambsel</code> is unnecessary and trivial (if needed, we could define <code>Ambsel</code> as the identity operation and make these examples slightly more verbose). However, this implementation should be refactored to extract the example logic from the implementation of <code>amb</code> (or you can do as was apparently suggested here, and use the definition of <code>amb</code> instead of the word - replacing <code>success</code> as needed).
└────┴─────┴─────┴──────┘
+-----------------------+</syntaxhighlight>
 
=={{header|JavaScript}}==
6,951

edits