节目说明
我的程序用于计算20X15尺寸平面中的形状位置.我有一个形状列表,其中包含形状类型,其id,半径或高度,以及它在平面上的预期[X,Y]位置.我有一个不同的二进制操作列表,只包含形状类型,它的id,以及它与另一个形状的位置关系.通过操作列表中的这些信息,我应该计算形状的[X,Y]位置:下面是两个列表的描述:
形状列表
我有一个形状列表:每个形状是一个表单列表:
[[shape, id],height/radius, [X,Y]]
当Prolog打印出来时,这些形状的列表看起来如下所示:
[[[diamond,1],4,[_7948,_7954]],[[circle,3],6,[_7894,_7900]],[[square,1],4,[_7840,_7846]],[[circle,1],5,[_7786,_7792]]|_7800]
Run Code Online (Sandbox Code Playgroud)
运营清单
应对每个操作的形状执行的操作列表如下:
[[[circle,1],below,[square,1]]]
Run Code Online (Sandbox Code Playgroud)
这意味着圆1应出现在X,Y平面上的方形1下方
当prolog打印出来时,这样的列表看起来如下所示:
[[[circle,1],below,[square,1]]|_8016]
Run Code Online (Sandbox Code Playgroud)
所以我有 computeShapeLocations/2
.它的第一个参数是一个操作列表,第二个列表是一个形状列表.它以递归方式遍历操作列表,在操作的两侧获取形状ID.例如circle 1 - below - sqaure 1
,将两个形状发送到正确的函数,以使用CLPFD计算位置.对于两个相对定位为"低于"的形状,我使用的computeShapesBelow/2
形状各有两种形状[[shape, id],height/radius, [X,Y]]
.
ComputeShapeLocations/2中的步骤:
1.从操作列表中获取[[[circle,1],below,[square,1]]]形式的操作2.获取第一个id(圆圈1),然后获取关系类型(下面)然后是第二个id(方1).3.从形状列表中获取形状(ShapesOut)4.将形状发送到computeShapesBelow/2
.这只是使用clpfd来比较半径或高度以及我的X,Y平面的尺寸.
:- use_module(library(clpfd)).
computeShapeLocations([],_ShapesOut).
computeShapeLocations([Operation|Rest],ShapesOut) :- writeln(ShapesOut),
writeln([Operation|Rest]),
nth0(0,Operation,Subject1),
nth0(1,Operation,below),
nth0(2,Operation,Subject2),
Shape1 = [Subject1,H,Loc],
Shape2 = [Subject2,H2,Loc2],
member(Shape1,ShapesOut),
member(Shape2,ShapesOut),
writeln(Shape1),
writeln(Shape2),
writeln(Subject1),
writeln(Subject2),
computeShapeBelow(Shape1,Shape2),
computeShapeLocations(Rest,ShapesOut).
computeShapeBelow(Shape1,Shape2) :- nth0(2,Shape1,Location1),
nth0(2,Shape2,Location2),
writeln(Shape1),
writeln(Shape2),
nth0(1,Shape1,Dim1),
nth0(1,Shape2,Dim2),
nth0(0,Location1,Xcord1),
nth0(0,Location2,Xcord2),
nth0(1,Location1,Ycord1),
nth0(1,Location2,Ycord2),
Ycord1 #> Dim1, Ycord1 #< 15-Dim1, …
Run Code Online (Sandbox Code Playgroud) 我试图用承诺/未来扩展Pharo.我遇到了这个网站http://onsmalltalk.com/smalltalk-concurrency-playing-with-futures.它在Smalltalk实现期货.但是,当我将这部分代码复制到Pharo上时,我遇到了一些错误:
value: aBlock
promiseLock := Semaphore new.
[ [ promiseValue := aBlock value ]
on: Error
do: [ :err | promiseError := err ]
ensure: [ promiseLock signal ] ] forkBackground
Run Code Online (Sandbox Code Playgroud)
这些是错误:
[forkBackground] Messages sent but not implemented
[on:do:ensure:] Messages sent but not implemented
Run Code Online (Sandbox Code Playgroud)
我认为Pharo与Smalltalk没有什么不同,或者网站的解决方案是否可能与Smalltalk不兼容?
我正在法鲁执行期货。我偶然发现了这个网站http://onsmalltalk.com/smalltalk-concurrency-playing-with-futures。我正在关注此示例,并尝试在Pharo上复制它。但是,我到了最后一步,我不知道“ >>”是什么意思:http://rigaux.org/language-study/syntax-across- language-per-language / Smalltalk.html。
BlockClosure>>future
^ SFuture new value: self fixTemps
Run Code Online (Sandbox Code Playgroud)
我可以看到future不是由BlockClosure实现的变量或方法。我应该如何处理这部分代码,以使承诺/未来工作如http://onsmalltalk.com/smalltalk-concurrency-playing-with-futures所示?我不能按原样将其添加到Playground或作为Promise类的方法添加,还是缺少任何内容?
在将未来的方法添加到BlockClosure之后,这是我在PlayGround上尝试的代码。
value1 := [200 timesRepeat:[Transcript show: '.']. 6] future.
value2 := [200 timesRepeat:[Transcript show: '+']. 6] future.
Transcript show: 'other work'.
Transcript show: (value1 + value2).
Date today
Run Code Online (Sandbox Code Playgroud)
成绩单显示以下错误,而不是预期值12。
UndefinedObject>>DoIt (value1 is Undeclared)
UndefinedObject>>DoIt (value2 is Undeclared)
Run Code Online (Sandbox Code Playgroud) 我有以下代码,嵌套循环有一个问题:我的目标是为学术目的实现CLOS多分派(多方法).我有一个传递给泛型函数的参数列表.泛型函数(gf)包含方法列表.反过来,泛型函数中的每个方法都包含它所操作的参数所属的类(特化器)列表.对于适用的方法,传递给泛型函数的每个参数必须是泛型函数的方法列表中包含的方法中其各自的特化器的实例或子类.特别是,使用局部变量并在嵌套循环中返回它们是一个问题.
(defun compute-applicable-methods (gf &rest args)
(loop for method in (generic-function-methods gf)
do (loop for specializer in (method-specializer method)
for arg in args
counting (instancep arg specializer) into matched_args
)
when (= matched_args (count args)
collect method ) ))
Run Code Online (Sandbox Code Playgroud) 我有这个功能:
(defun call-generic-function (gf &rest args)
(let* ((applicable-methods (compute-applicable-methods gf args))
(most-specific-method (select-next-method-to-call
applicable-methods)))
(print "applicable methods")
(print (length applicable-methods))
(print args)
(print (values-list args))
(funcall (method-function most-specific-method)
(values-list args)
(remove most-specific-method applicable-methods))))
Run Code Online (Sandbox Code Playgroud)
我期望values-list
给我列表中的所有元素args
(这两个对象要通过funcall作为被调用方法的三个预期参数中的两个传递)但它只给出第一个元素并且我得到一个错误,我只是提供2个参数而不是预期的3个参数.这可以通过(print args)
打印2个对象的列表但(print (values-list args))
仅打印出第一个对象的事实来确认 .我怎么能纠正这个?
结果(打印参数):
(#S(OBJECT
:CLASS #S(CLASS
:DIRECT-SUPERCLASS #S(CLASS
:DIRECT-SUPERCLASS #S(CLASS
:DIRECT-SUPERCLASS NIL
:DIRECT-SLOTS NIL)
:DIRECT-SLOTS (NAME ADDRESS))
:DIRECT-SLOTS (EMPLOYER))
:SLOTS #<HASH-TABLE :TEST EQL :COUNT 3 {1003932403}>)
#S(OBJECT
:CLASS #S(CLASS
:DIRECT-SUPERCLASS #S(CLASS
:DIRECT-SUPERCLASS NIL
:DIRECT-SLOTS NIL)
:DIRECT-SLOTS …
Run Code Online (Sandbox Code Playgroud)