我只是看着map :: (a -> b) -> [a] -> [b]这个函数的类型和形状让我想知道我们是否能够看到列表形成operator []遵循正常模态逻辑(例如,T,S4,S5,B)的各种公理,因为我们似乎至少拥有正常模态逻辑的K-公理[(a -> b)] -> [a] -> [b].
这引出了我的问题:在Haskell中是否有熟悉的,有趣的运算符或函子,它们具有某种模态运算符的语法,并且遵循普通模态逻辑(即K,T,S4,S5和B)常见的公理. )?
这个问题可以更加清晰,更具体.考虑一个运营商L,它的双重M.现在问题变成了:Haskell中是否有任何熟悉,有趣的运算符,具有以下某些属性:
(1) L(a -> b) -> La -> Lb
(2) La -> a
(3) Ma -> L(M a)
(4) La -> L(L a)
(5) a -> L(M a)
看到一些很好的例子会非常有趣.
我已经想到了一个潜在的例子,但是知道我是否正确是很好的:带有Las not not和Mas 的双重否定翻译not.这种翻译将每个公式都a用于其双重否定翻译,(a -> ?) -> ?并且至关重要的是,验证公理(1) - (4),但不是公理(5).我在这里问了一个问题https://math.stackexchange.com/questions/2347437/continuations-in-mathematics-nice-examples 似乎可以通过continuation monad模拟双重否定翻译,endofunctor将每个公式都a …
我如何可以搜索类型length, take, drop, splitAt, !!,并replicate全部一气呵成,没有进入:t length,:t take,:t drop,等等,所有的这些功能呢?
我按照http://www.euterpea.com此处的说明安装 Euterpea(一种用于嵌入 Haskell 的计算机音乐应用程序的域特定语言),我收到一条消息,指出某些软件包和东西无法安装(请参阅下面的代码) )。如何安装剩余的软件包?
Mo-MacBook-Pro:~ Mo$ cabal update
Downloading the latest package list from hackage.haskell.org
cabal install Euterpea
To revert to previous state run:
cabal update --index-state='2018-06-25T08:34:14Z'
Mo-MacBook-Pro:~ Mo$ cabal install Euterpea
clang: warning: argument unused during compilation: '-nopie' [-Wunused-
command-line-argument]
Resolving dependencies...
Downloading heap-1.0.4...
Downloading PortMidi-0.1.6.1...
Configuring PortMidi-0.1.6.1...
Downloading lazysmallcheck-0.6...
Configuring heap-1.0.4...
Downloading semigroups-0.18.5...
Configuring lazysmallcheck-0.6...
Configuring semigroups-0.18.5...
Building heap-1.0.4...
Building PortMidi-0.1.6.1...
Building lazysmallcheck-0.6...
Building semigroups-0.18.5...
Installed semigroups-0.18.5
Downloading stm-2.5.0.0...
Configuring stm-2.5.0.0...
Installed lazysmallcheck-0.6
Downloading HCodecs-0.5.1... …Run Code Online (Sandbox Code Playgroud) 每当我在终端上打开emacs文件时,都会出现以下类型的消息:
mm-Pro:~ mm$ emacs experiment.hs
2019-02-21 15:56:19.243 Emacs[2937:261832] Failed to initialize color
list unarchiver: Error Domain=NSCocoaErrorDomain Code=4864 "*** -
[NSKeyedUnarchiver_initForReadingFromData:error:throwLegacyExceptions:]:
non-keyed archive cannot be decoded by NSKeyedUnarchiver" UserInfo=
{NSDebugDescription=*** -[NSKeyedUnarchiver _initForReadingFromData:error:
throwLegacyExceptions:]: non-keyed archive cannot be decoded by NSKeyedUnarchiver}
Run Code Online (Sandbox Code Playgroud)
问题是什么?这是否与emacs保持冻结在我的计算机上的事实有任何联系?
我刚刚尝试使用终端使用 Home-brew 安装 Anaconda,并收到以下消息:
L-MBP:agda-stdlib le$ brew install conda
Updating Homebrew...
Error: No available formula with the name "conda"
==> Searching for a previously deleted formula (in the last month)...
Warning: homebrew/core is shallow clone. To get complete history run:
git -C "$(brew --repo homebrew/core)" fetch --unshallow
Error: No previously deleted formula found.
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
==> Searching taps...
==> Searching taps on GitHub...
Error: No formulae found in taps.
L-MBP:agda-stdlib …Run Code Online (Sandbox Code Playgroud) 在 Haskell 中,我们可以展平列表列表展平列表列表
对于元组的简单情况,我可以看到我们将如何展平某些元组,如下例所示:
flatten :: (a, (b, c)) -> (a, b, c)
flatten x = (fst x, fst(snd x), snd(snd x))
flatten2 :: ((a, b), c) -> (a, b, c)
flatten2 x = (fst(fst x), snd(fst x), snd x)
Run Code Online (Sandbox Code Playgroud)
但是,我正在寻找一个函数,该函数接受任何嵌套元组作为输入并将该元组展平。
可以在 Haskell 中创建这样的函数吗?
如果不能创建,为什么会这样?
我在emacs文件上定义了以下函数(取自http://www.happylearnhaskelltutorial.com/1/shop_for_food_with_list.html#s9):
firstOnesOrEmpty :: [String] -> String
firstOnesOrEmpty [] = ""
firstOnesOrEmpty [x] = x
firstOnesOrEmpty (x:y:_) = x ++ ", " ++ y
Run Code Online (Sandbox Code Playgroud)
但是,当我将文件加载到GHCi并写入:t firstOnesOrEmptyGHCi时,出现以下错误:
<interactive>:1:1: error: Variable not in scope: firstOnesOrEmpty
Run Code Online (Sandbox Code Playgroud)
怎么了?
我在emacs文件中定义的另一个功能(同样来自上面的网站)也有类似的问题:
joinedWithCommas :: [String] -> String
joinedWithCommas [] = ""
joinedWithCommas [x] = x
joinedWithCommas (x:xs) = x ++ ", " ++ joinedWithCommas xs
Run Code Online (Sandbox Code Playgroud)
尝试在GHCi中使用此功能,我得到:
"ghci>" joinedWithCommas []
<interactive>:40:1: error:
Variable not in scope: joinedWithCommas :: [a0] -> t
"ghci>" joinedWithCommas [x]
<interactive>:41:1: …Run Code Online (Sandbox Code Playgroud) 下面的一段代码(取自Graham Hutton的"Haskell编程"(第9章))
:{
Prelude| data Op = Add | Sub | Mul | Div
Prelude| instance Show Op where
Prelude| show Add = "+"
Prelude| show Sub = "-"
Prelude| show Mul = "*"
Prelude| show Div = "/"
Prelude| valid :: Op -> Int -> Int -> Bool
Prelude| valid Add _ _ = True
Prelude| valid Sub x y = x > y
Prelude| valid Mul _ _ = True
Prelude| valid Div x y = x `mod` …Run Code Online (Sandbox Code Playgroud) 有没有办法shift通过重写下面的实例来找到下面代码中的类型?当我尝试在下面调用它时,我找不到它的类型是因为它是本地绑定(在 where 构造中)吗?
class CoMonad m where
extract :: m a -> a
(<<=) :: (m a -> b) -> m a -> m b
instance (Monoid s) => CoMonad ((->) s) where
extract = ($ mempty)
f <<= r = \ t -> (f . shift t) r
where shift t rr = \ s -> rr (s <> t)
:t shift
<interactive>:1:1: error: Variable not in scope: shift
Run Code Online (Sandbox Code Playgroud) 为什么下面的代码会产生错误parse error on input ‘putStrLn’?
main = do line <- fmap reverse getLine
putStrLn $ "You said " ++ line ++ " backwards!"
putStrLn $ "Yes, you said " ++ line ++ " backwards!"
<interactive>:11:4: error: parse error on input ‘putStrLn’
Run Code Online (Sandbox Code Playgroud)
另外,为什么以下代码会产生错误parse error on input ‘let’?
main = do line <- getLine
let line' = reverse line
putStrLn $ "You said " ++ line' ++ " backwards!"
putStrLn $ "Yes, you said " ++ line' ++ …Run Code Online (Sandbox Code Playgroud) 我在尝试在这里实现 Chris Penner 的代码时遇到了问题https://github.com/ChrisPenner/comonads-by-example/blob/master/docs/rendered/01-streams.pdf。
具体来说,在下面的一段代码中:
data Stream a = a :> Stream a
deriving (Functor, Foldable)
{-instance Functor Stream where
f(x:>xs) = (f x):>(fmap f xs) -}
fromlist :: [a] -> Stream a
fromlist xs = go (cycle xs)
where
go (a: rest)= a :> go rest
countStream :: Stream Int
countStream = fromlist [0..]
Run Code Online (Sandbox Code Playgroud)
这会产生以下错误:
comonads.hs:17:16: error:
• Can't make a derived instance of ‘Functor Stream’:
You need DeriveFunctor to derive an instance for this class
• …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Haskell 中的幺半群,使用此页面:https : //en.wikibooks.org/wiki/Haskell/Monoids。我在终端输入以下信息(导入后Data.Monoid):
class Monoid a where
mempty :: a
mappend :: a -> a -> a
mconcat :: [a] -> a
mconcat = foldr mappend memptyhere
newtype Sum a = Sum { getSum :: a }
instance Num a => Monoid (Sum a) where
mempty = Sum 0
Sum x `mappend` Sum y = Sum (x + y)
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试时Sum 5 <> Sum 6 <> Sum 10,我收到以下消息:
<interactive>:115:1: error:
• Non type-variable …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个类似的函数map,但它将类型函数(a, a) -> b作为其第一个参数.但是,我得到了错误
<interactive>:474:11: error:
Parse error in pattern: \ (x, y) -> f x y
Run Code Online (Sandbox Code Playgroud)
使用以下代码:
Prelude> :{
Prelude| mappairs :: ((a, a) -> b) -> [a] -> [b]
Prelude| mappairs (\(x,y) -> f x y) xs = foldr (\(x, y) acc -> (f x y : acc)) [] xs
Prelude| :}
Run Code Online (Sandbox Code Playgroud)
问题是什么?
haskell ×11
anaconda ×1
do-notation ×1
dsl ×1
emacs ×1
euterpea ×1
ghci ×1
homebrew ×1
io-monad ×1
modal-logic ×1
monoids ×1
python ×1
syntax-error ×1