我可以定义一个" rec"在另一个" rec" Ocaml中的功能?

时间:2016-10-21 23:58:37

标签: function recursion ocaml

我必须创建一个函数isFunction,它将一对夫妇列表作为一个参数,如果没有重复考虑每一对中的第一个元素则返回true,如果有重复则返回false。

例如:isFunction [(1,3);(2,40);(3,40)]返回trueisFunction [(1,3);(2,40);(1,40)]返回false,因为重复了1。

目前,我的代码是:

let rec exist e = function
    |[] -> false
    |(a,_)::l -> e=a || exist e l;;
let rec isFunction = function
    |[]->true
    |(a,_)::l -> not(exist a l) && isFunction l;;

完美无缺!但问题是:是否有另一种方法来定义isFunction而不定义另一个辅助函数?

2 个答案:

答案 0 :(得分:1)

您可以将exist声明为isFunction的本地人:

let rec isFunction l =
  let rec exist e = function
    |[] -> false
    |(a,_)::l -> e=a || exist e l
  in
  match l with
   |[]->true
   |(a,_)::l -> not(exist a l) && isFunction l

答案 1 :(得分:1)

您可以使用List.exists:

[2016-10-22 00:57:30,572]  INFO - LogMediator STATUS = Message dispatched to the main sequence. Invalid URL., RESOURCE = /appInstallOrRemove
[2016-10-22 00:57:30,579] ERROR - RelayUtils Error while building Passthrough stream
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(String.java:1967)
    at org.apache.synapse.commons.builders.XFormURLEncodedBuilder.extractParametersFromRequest(XFormURLEncodedBuilder.java:223)
    at org.apache.synapse.commons.builders.XFormURLEncodedBuilder.processDocumentWrapper(XFormURLEncodedBuilder.java:128)
    at org.apache.synapse.commons.builders.XFormURLEncodedBuilder.processDocument(XFormURLEncodedBuilder.java:52)
    at org.apache.synapse.transport.passthru.util.DeferredMessageBuilder.getDocument(DeferredMessageBuilder.java:148)
    at org.apache.synapse.transport.passthru.util.RelayUtils.builldMessage(RelayUtils.java:137)
    at org.apache.synapse.transport.passthru.util.RelayUtils.buildMessage(RelayUtils.java:100)
    at org.apache.synapse.mediators.AbstractListMediator.buildMessage(AbstractListMediator.java:127)
    at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:81)
    at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:57)
    at org.apache.synapse.config.xml.AnonymousListMediator.mediate(AnonymousListMediator.java:37)
    at org.apache.synapse.mediators.filters.FilterMediator.mediate(FilterMediator.java:203)
    at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:95)
    at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:57)
    at org.apache.synapse.mediators.filters.InMediator.mediate(InMediator.java:74)
    at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:95)
    at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:57)
    at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:158)
    at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:310)
    at org.apache.synapse.core.axis2.SynapseMessageReceiver.receive(SynapseMessageReceiver.java:75)
    at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
    at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:319)
    at org.apache.synapse.transport.passthru.ServerWorker.processEntityEnclosingRequest(ServerWorker.java:365)
    at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:145)
    at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
[2016-10-22 00:57:30,582] ERROR - AnonymousListMediator Error while building message
org.apache.axis2.AxisFault: Error while building Passthrough stream
    at org.apache.synapse.transport.passthru.util.RelayUtils.handleException(RelayUtils.java:287)
    at org.apache.synapse.transport.passthru.util.RelayUtils.builldMessage(RelayUtils.java:146)
    at org.apache.synapse.transport.passthru.util.RelayUtils.buildMessage(RelayUtils.java:100)
    at org.apache.synapse.mediators.AbstractListMediator.buildMessage(AbstractListMediator.java:127)
    at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:81)
    at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:57)
    at org.apache.synapse.config.xml.AnonymousListMediator.mediate(AnonymousListMediator.java:37)
    at org.apache.synapse.mediators.filters.FilterMediator.mediate(FilterMediator.java:203)
    at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:95)
    at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:57)
    at org.apache.synapse.mediators.filters.InMediator.mediate(InMediator.java:74)
    at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:95)
    at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:57)
    at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:158)
    at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:310)
    at org.apache.synapse.core.axis2.SynapseMessageReceiver.receive(SynapseMessageReceiver.java:75)
    at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
    at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:319)
    at org.apache.synapse.transport.passthru.ServerWorker.processEntityEnclosingRequest(ServerWorker.java:365)
    at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:145)
    at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(String.java:1967)
    at org.apache.synapse.commons.builders.XFormURLEncodedBuilder.extractParametersFromRequest(XFormURLEncodedBuilder.java:223)
    at org.apache.synapse.commons.builders.XFormURLEncodedBuilder.processDocumentWrapper(XFormURLEncodedBuilder.java:128)
    at org.apache.synapse.commons.builders.XFormURLEncodedBuilder.processDocument(XFormURLEncodedBuilder.java:52)
    at org.apache.synapse.transport.passthru.util.DeferredMessageBuilder.getDocument(DeferredMessageBuilder.java:148)
    at org.apache.synapse.transport.passthru.util.RelayUtils.builldMessage(RelayUtils.java:137)
    ... 22 more
[2016-10-22 00:57:30,583]  INFO - LogMediator STATUS = Executing default 'fault' sequence, ERROR_CODE = 0, ERROR_MESSAGE = Error while building message
[2016-10-22 00:57:30,586] ERROR - ServerWorker Error processing POST reguest for : /appInstallOrRemove. Error detail: org.apache.synapse.SynapseException: Error occured in the mediation of the class mediator. 
java.lang.RuntimeException: org.apache.synapse.SynapseException: Error occured in the mediation of the class mediator
    at org.apache.synapse.FaultHandler.handleFault(FaultHandler.java:108)
    at org.apache.synapse.core.axis2.SynapseMessageReceiver.receive(SynapseMessageReceiver.java:81)
    at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
    at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:319)
    at org.apache.synapse.transport.passthru.ServerWorker.processEntityEnclosingRequest(ServerWorker.java:365)
    at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:145)
    at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.synapse.SynapseException: Error occured in the mediation of the class mediator
    at org.apache.synapse.mediators.ext.ClassMediator.mediate(ClassMediator.java:88)
    at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:95)
    at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:57)
    at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:158)
    at org.apache.synapse.mediators.MediatorFaultHandler.onFault(MediatorFaultHandler.java:93)
    at org.apache.synapse.FaultHandler.handleFault(FaultHandler.java:101)
    ... 9 more
Caused by: java.lang.NullPointerException
    at org.wso2.carbon.apimgt.impl.utils.APIUtil.getAPIProviderFromRESTAPI(APIUtil.java:5217)
    at org.wso2.carbon.apimgt.usage.publisher.APIMgtCommonExecutionPublisher.mediate(APIMgtCommonExecutionPublisher.java:50)
    at org.wso2.carbon.apimgt.usage.publisher.APIMgtFaultHandler.mediate(APIMgtFaultHandler.java:20)
    at org.apache.synapse.mediators.ext.ClassMediator.mediate(ClassMediator.java:84)
    ... 14 more