我是prolog的新手,想要将所有查询保存在文件中,而不是手动输入.
我有这些事实facts.pl
:
likes(wallace, cheese).
likes(grommit, cheese).
likes(wendolene, sheep).
friend(X, Y) :- \+(X = Y), likes(X, Z), likes(Y, Z).
Run Code Online (Sandbox Code Playgroud)
在阅读了这个问题的答案后,我想出了以下代码queries.pl
:
main :-
write(likes(wallace, cheese)),
halt.
:- initialization(['facts.pl']).
:- initialization(main).
Run Code Online (Sandbox Code Playgroud)
在这里,我想检查是否likes(wallace, cheese)
保持,我期望输出类似yes
或no
实际输出的东西likes(wallace, cheese)
我已经google了很多次尝试
X = likes(wallace, cheese), write(X).
X is likes(wallace, cheese), write(X).
X := likes(wallace, cheese), write(X).
但它们都不起作用.
对你来说这可能是一个非常简单的问题,但我不知道如何把事情弄清楚.
顺便说一下,我正在使用GNU Prolog 1.4.1
举一个简单的例子,假设我想要一个类型来表示井字游戏标记:
data Mark = Nought | Cross
Run Code Online (Sandbox Code Playgroud)
这与 Bool
Prelude> :info Bool
data Bool = False | True -- Defined in ‘GHC.Types’
Run Code Online (Sandbox Code Playgroud)
但是Coercible Bool Mark
它们之间没有,即使我导入GHC.Types
(我最初认为 GHC 可能需要Bool
定义可见的位置),获得此实例的唯一方法似乎是通过newtype
.
也许我可以这样来定义newtype Mark = Mark Bool
和界定Nought
,并Cross
具有双向模式,我希望有东西比这更简单。
我想判断两个值是否属于同一类型,但我发现空列表的类型clojure.lang.PersistentList$EmptyList
不是clojure.lang.PersistentList
.
user=> (def la '())
#'user/la
user=> (def lb '(1 2))
#'user/lb
user=> (def t (map type [la lb]))
#'user/t
user=> t
(clojure.lang.PersistentList$EmptyList clojure.lang.PersistentList)
user=> (apply = t)
false
user=>
Run Code Online (Sandbox Code Playgroud)
所以,我想知道为什么空列表的类型与非空列表的类型不同,以及判断两个事物是否属于同一类型的正确方法是什么?
我刚刚在OS X 10.9上安装了GLFW.已安装标头并安装/usr/local/include
了库/usr/local/lib
.
我想知道还有什么方法可以让我的C++程序包含标题,#include "GLFW/glfw3.h"
而不是指定整个路径#include "usr/local/include/GLFW/glfw3.h"
.
同样的事情是图书馆,因为到目前为止,我甚至无法使用链接库-lglfw3
.提前致谢.
给定类型的函数f :: a -> a
,我们可以生成一个适用f
于n
时间的函数:
nTimes :: Int -> (a -> a) -> (a -> a)
nTimes 0 _ = id
nTimes 1 f = f
nTimes n f = f . nTimes (n-1) f
Run Code Online (Sandbox Code Playgroud)
我可以通过平方法在这里使用取幂来实现另一个nTimes
函数:
nTimes' :: Int -> (a -> a) -> (a -> a)
nTimes' = nTimes'' id
where
nTimes'' acc n f
| n == 0 = acc
| even n = nTimes'' acc …
Run Code Online (Sandbox Code Playgroud) 这可能不是一个非常实际的问题,我只是好奇我是否可以实现只有lambda表达式的堆栈.
堆栈支持3个操作:top
,pop
和push
,因此,我首先定义堆栈是一个3元组:
data Stack a = Stack a (a -> Stack a) (Stack a)
| Empty
Run Code Online (Sandbox Code Playgroud)
这里Empty
代表空堆,所以我们至少有一个居民开始.
根据这个定义,除了push
操作之外,eveything看起来很好:
import Control.Monad.State
import Control.Monad.Writer
import Data.Maybe
data Stack a = Stack a (a -> Stack a) (Stack a)
| Empty
safePop :: Stack a -> Maybe (Stack a)
safePop Empty = Nothing
safePop (Stack _ _ s) = Just s
safeTop :: Stack a -> Maybe a
safeTop Empty = Nothing
safeTop …
Run Code Online (Sandbox Code Playgroud) 我创建了一个玩具程序,试图在升级类型上做分支:
{-# LANGUAGE KindSignatures, DataKinds, TypeFamilies, ScopedTypeVariables, GADTs #-}
module Foo where
import Data.Proxy
data Out = ToInt | ToBool
type family F (a :: Out) where
F ToInt = Int
F ToBool = Bool
foo :: forall k. Proxy (k :: Out) -> Int -> F k
foo p = case p of
(Proxy :: Proxy 'ToInt) -> id
(Proxy :: Proxy 'ToBool) -> (== 0)
Run Code Online (Sandbox Code Playgroud)
在这里,我尝试分支Proxy
并在它们上使用显式类型签名,但这不起作用,GHC抱怨:
[1 of 1] Compiling Foo ( Foo.hs, Foo.o )
Foo.hs:15:6: …
Run Code Online (Sandbox Code Playgroud) 假设我有一个通过 Writer 发出软故障的 monad。简化版本如下(郑重声明,我使用的是 GHC 9.0.2):
\n{-# LANGUAGE BlockArguments #-}\n{-# LANGUAGE FlexibleContexts #-}\n-- {-# LANGUAGE GADTs #-}\n{-# LANGUAGE ScopedTypeVariables #-}\n\nmodule Lib where\n\nimport Control.Monad.Writer.CPS\nimport Data.Maybe\n\nverify :: MonadWriter [String] m => some -> args -> m ()\nverify _ _ = fix \\(_ :: m ()) ->\n do\n let warn = tell . (: [])\n a = Just 1 :: Maybe Int\n b = Just [1, 23] :: Maybe [Int]\n c = Just (id :: forall a. a -> a)\n -- isJust\' …
Run Code Online (Sandbox Code Playgroud) 我在阅读"编程F#3.0"时遇到了这段代码:
type BitCounter =
static member CountBits (x : int16) =
let mutable x' = x
let mutable numBits = 0
for i = 0 to 15 do
numBits <- numBits + int (x' &&& 1s)
x' <- x' >>> 1
numBits
static member CountBits (x : int) =
let mutable x' = x
let mutable numBits = 0
for i = 0 to 31 do
numBits <- numBits + int (x' &&& 1)
x' <- x' >>> 1
numBits …
Run Code Online (Sandbox Code Playgroud) 我想制作一个玩具功能,产生一个Maybe a
,然后举起show
来使它成为一个Maybe String
,但结果对我来说很奇怪:
?> :t liftM show . Just
liftM show . Just :: Show a1 => a1 -> Maybe String
?> liftM show . Just $ 10
Just "10"
?> let f = liftM show . Just
?> f 10
<interactive>:9:3:
No instance for (Num ()) arising from the literal `10'
Possible fix: add an instance declaration for (Num ())
In the first argument of `f', namely `10'
In the expression: f 10 …
Run Code Online (Sandbox Code Playgroud)