我正在编写一个关于音程分类的程序.概念结构非常复杂,我会尽可能清楚地表达它.前几行代码是一个正常工作的小提取.第二个是伪代码,可以满足我的简洁需求.
interval pt1 pt2
| gd == 0 && sd < (-2) = ("unison",show (abs sd) ++ "d")
| gd == 0 && sd == (-2) = ("unison","dd")
| gd == 0 && sd == (-1) = ("unison","d")
| gd == 0 && sd == 0 = ("unison","P")
| gd == 0 && sd == 1 = ("unison","A")
| gd == 0 && sd == 2 = ("unison","AA")
| gd == 0 && sd > 2 = ("unison",show sd ++ …Run Code Online (Sandbox Code Playgroud) 我构建了一个函数来验证可折叠结构的所有元素是否相等。
与清单上的类似功能相比,在我看来,更通用的功能非常复杂,但是我无法对其进行简化。
你有什么建议吗?
import Data.Monoid
import Data.Sequence as SQ
import Data.Matrix as MT
allElementsEqualL :: Eq a => [a] -> Bool
allElementsEqualL [] = True
allElementsEqualL (x:ns) = all (== x) ns
-- allElementsEqualL [1,1,1] -> True
allElementsEqualF :: (Foldable t, Eq a) => t a -> Bool
allElementsEqualF xs = case (getFirst . foldMap (First . Just) $ xs) of
Nothing -> True
Just x -> all (== x) xs
-- allElementsEqualF [1,1,1] -> True
-- allElementsEqualF $ SQ.fromList …Run Code Online (Sandbox Code Playgroud) 我正在尝试将Relation ab定义为Category实例。在我看来,作曲家的定义明确,并遵守关联法。当涉及到ID时,我找不到正确的定义。我究竟做错了什么?
import Data.Map as M
import Data.Set as S
import Control.Category as Cat
newtype Relation a b = R (Map a (Set b)) deriving (Show, Eq)
-- instance Cat.Category Relation where
-- id =
-- (.) = (°)
-- GHC.Base.id r1
-- > R (fromList [(10,fromList "abef"),(30,fromList "GRTXa")])
r1 = R $ M.fromList [(10,S.fromList "abfe"),(30,S.fromList "aXGRT")]
r2 = R $ M.fromList [('a',S.fromList [Just "Apple",Just "Ask"]),('b',S.fromList [Just "book",Just "brother"]),('T',S.fromList [Just "Table"]),('?',S.fromList [Just "Apple",Just "brother"])]
-- ex. r1 ° r2 = …Run Code Online (Sandbox Code Playgroud) 下面定义的函数mapRightR仅更改地图的设置内容,而不更改键,并产生有效的Relation类型。
使用此高级函数定义Functor Relation实例真的是不可能的,还是我的实现错误。
{-# LANGUAGE GADTs #-}
import Data.Map as M
import Data.Set as S
data Relation a b where
R :: (Ord a, Ord b) => Map a (Set b) -> Relation a b
instance Functor Relation where
fmap f r = mapRightR f r
mapRightR :: Ord b1 => (b2 -> b1) -> Relation a b2 -> Relation a b1
mapRightR f (R r) = R $ M.map (S.map f) r
Run Code Online (Sandbox Code Playgroud)
谢谢,切普纳。
我尝试了关系的另一个定义,使用List …
关于管理.exe文件生成中的unicode字符的第一个问题,这也是GHC中的一个错误?
> print "Frère"
"Fr\233re"
Run Code Online (Sandbox Code Playgroud) 我需要Data.E v.4.7.0.0的新函数"isLeft",但cabal不会安装新库.
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\Alberto>cabal update
Downloading the latest package list from hackage.haskell.org
Note: there is a new version of cabal-install available.
To upgrade, run: cabal install cabal-install
C:\Users\Alberto>cabal install base
Resolving dependencies...
All the requested packages are already installed:
base-4.6.0.1
Use --reinstall if you want to reinstall anyway.
C:\Users\Alberto>date
The current date is: 19/04/2014
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
如何将基础软件包升级到4.7版?
1)我需要将字段构造函数参数传递给函数。我做了一些测试,但我无法这样做。是否可以?否则,是否可以使用镜头包装?
2) 是否可以在 MonadState 中使用修改来修改字段?(我做了几次尝试,但没有成功。例如: modify (second = "x") 不起作用。
import Control.Monad.State
data Test = Test {first :: Int, second :: String} deriving Show
dataTest = Test {first = 1, second = ""}
test1 = runStateT modif1 dataTest -- OK
test2 = runStateT (modif2 "!") dataTest -- OK
test3 = runStateT (modif3 second) dataTest -- WRONG
-- modif1 :: StateT Test IO ()
modif1 = do
st <- get
r <- lift getLine
put $ st {second = "x" ++ …Run Code Online (Sandbox Code Playgroud) 尽管多次尝试,Cabal都没有更新.为什么Cabal不能正常工作?我该怎么办?最后更新的日期可追溯至89天.
Windows PowerShell
Copyright (C) Microsoft Corporation. Tutti i diritti sono riservati.
PS C:\WINDOWS\system32> ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.2.2
PS C:\WINDOWS\system32> cabal --version
cabal-install version 2.0.0.1
compiled using version 2.0.1.1 of the Cabal library
PS C:\WINDOWS\system32>
PS C:\WINDOWS\system32> cabal update
Downloading the latest package list from hackage.haskell.org
dieVerbatim: user error (cabal.exe: Failed to download
http://objects-us-west-1.dream.io/hackage-mirror/01-index.tar.gz : No Status
Code could be parsed from response: Eccezione durante una richiesta WebClient.
)
Run Code Online (Sandbox Code Playgroud) 为什么Monoid实例不需要(Ord a,Ord b)约束,而Semigroup实例却不需要?
这是否取决于Category.Constrained类或使用GADT定义数据类型?
{-# LANGUAGE GADTs, TypeFamilies, ConstraintKinds, StandaloneDeriving #-}
module Question3 where
import Control.Category.Constrained as CC
import Data.Set as S
import Data.Map as M
data RelationMS a b where
IdRMS :: RelationMS a a
RMS :: (Ord a, Ord b) => Map a (Set b) -> RelationMS a b
deriving instance (Show a, Show b) => Show (RelationMS a b)
RMS mp2 `compRMS` RMS mp1
| M.null mp2 || M.null mp1 = RMS M.empty
| otherwise = RMS …Run Code Online (Sandbox Code Playgroud) 我必须用(++)压缩n个列表.我发现在列表上定义此运算符没有更好的方法:
(+++) = zipWith (\x y -> x ++ " " ++ y)
Run Code Online (Sandbox Code Playgroud)
还有更好的解决方案吗?
在Markus1189回答之后,我提出了一个更普遍的问题:确实存在这样的函数:
zipNWith f2 [a] = f2 (f2 x1 x2) x3 ... f2 (xn-2 xn-1) xn?
Run Code Online (Sandbox Code Playgroud)
这是运算符使用的程序(+++)
csoundLines instrs inits durs amps freqs = putStrLn . unlines $
repeat "i" +++ ms instrs +++ ms inits +++ ms durs +++ ms amps +++ ms freqs
where
(+++) = zipWith (\x y -> x ++ " " ++ y)
ms xs = map show xs
scaleNTonic n …Run Code Online (Sandbox Code Playgroud) 以下是根据Petr Pudlak对我之前的问题的解决方案,在MonadState上对镜片(Edward Kmett)的一系列示例/练习.
除了展示镜片的一些用途和功能之外,这些例子还说明了理解GHCi生成的类型签名是多么困难.有希望将来事情会有所改善吗?
{-# LANGUAGE TemplateHaskell, RankNTypes #-}
import Control.Lens
import Control.Monad.State
---------- Example by Petr Pudlak ----------
-- | An example of a universal function that modifies any lens.
-- It reads a string and appends it to the existing value.
modif :: Lens' a String -> StateT a IO ()
modif l = do
s <- lift getLine
l %= (++ s)
-----------------------------------------------
Run Code Online (Sandbox Code Playgroud)
以下注释类型签名是GHCi生成的签名类型签名.另一个是彼得的改编.就个人而言,我很难理解GHCi产生的那些,我想知道:为什么GHCi不会产生那些简化的?
-------------------------------------------
-- modif2
-- :: (Profunctor p, MonadTrans t, MonadState s (t IO)) …Run Code Online (Sandbox Code Playgroud) 在列表中散布分隔符有两种可能性:
[x1, sep, x2, sep, .. xn]
[sep, x1, sep, x2, .. sep, xn]
在Data.List中使用"intersperse"函数:
?> intersperse 0 [1..5]
[1,0,2,0,3,0,4,0,5]
?> 0 : intersperse 0 [1..5]
[0,1,0,2,0,3,0,4,0,5]
Run Code Online (Sandbox Code Playgroud)
但是,使用隐藏(未导出)函数"prependToAll"可以简化第二种情况:
?> prependToAll 0 [1..5]
[0,1,0,2,0,3,0,4,0,5]
Run Code Online (Sandbox Code Playgroud)
为什么在Data.List库中导出了散布而而对于prepreToAll却没有?
通过示例简要介绍类型和事件.
EX1.
abbacb
a,b,c是类型.
a发生2次;b发生3次;c发生1次.
这可以更简洁地表示为[('a',2),('b',3),('c',1)](实际上,顺序无关紧要).
EX2.
abbacb
ab,bb,ba,ac,cb是的类型的序列每个序列只出现一次.
这可以表示为 [("ab",1),("bb",1),("ba",1),("ac",1),("cb",1)]
以下图形结构具有前两个相同的信息内容:
('a',2) -- 'a' occurs 2 times
('b',1) -- "ab" occurs 1 times
('c',1) -- "ac" occurs 1 times
('b',2) -- 'b' occurs 2 times
('a',1) -- "ba" occurs 1 times
('b',1) -- "bb" occurs 1 times
('c',1) -- 'c' occurs 1 times
('b',1) -- "cb" occurs …Run Code Online (Sandbox Code Playgroud)