我熟悉newtype声明:
newtype MyAge = Age {age :: Int} deriving (Show, Eq, Ord)
Run Code Online (Sandbox Code Playgroud)
在这个例子中,Age是一个Int,但是我遇到了下面的代码,我无法理解它:
newtype Ages a = Ages {age :: String -> [(a,String)]}
Run Code Online (Sandbox Code Playgroud)
这似乎是一个功能声明?(接受字符串,返回包含'a'和字符串的元组列表) - 这是正确的吗?
NB我刚刚意识到这只是声明一个函数的基本记录语法.
另外,我试图实现这种类型,但我必须做错事:
newtype Example a = Example {ex :: Int -> Int}
myexample = Example {ex = (\x -> x + 1)}
Run Code Online (Sandbox Code Playgroud)
这编译,但我不明白为什么因为我没有通过'a'参数?
我正在使用learnyouahaskell报道GADT,我对它们的可能用途很感兴趣.我知道它们的主要特征是允许显式类型设置.
如:
data Users a where
GetUserName :: Int -> Users String
GetUserId :: String -> Users Int
usersFunction :: Users a -> a
usersFunction (GetUserName id)
| id == 100 = "Bob"
| id == 200 = "Phil"
| otherwise = "No corresponding user"
usersFunction (GetUserId name)
| name == "Bob" = 100
| name == "Phil" = 200
| otherwise = 0
main = do
print $ usersFunction (GetUserName 100)
Run Code Online (Sandbox Code Playgroud)
除了在使用这些数据类型时增加额外的类型安全性,GADT的其他用途是什么?
考虑具有两个参数a和b的Users类型.显然,这使用户可以由两种不同的类型组成:
data Users a b = User a b deriving (Show, Eq, Ord)
Run Code Online (Sandbox Code Playgroud)
我们如何声明这个实例的仿函数和应用程序?
我试过这些方法不会编译:
instance Functor Users where
fmap f(User a b) = User (f a) (f b)
instance Applicative Users where
pure a b = User a b
(<*>) User a b = (fmap a) (fmap b)
Run Code Online (Sandbox Code Playgroud)
这些不能编译的原因是什么?
我很想知道 .includes() 方法使用什么算法?它是否使用像 rabin karp 这样的模块化哈希?
在不了解更多关于它的方法和速度的情况下,我对使用 .includes() 有点犹豫。我发现的文档在讨论时没有详细说明(例如https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes)
我有以下数据类型(来源:http://learnyouahaskell.com/zippers):
data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Show, Eq, Ord)
Run Code Online (Sandbox Code Playgroud)
然后我有以下函数,它遍历树并根据方向指令替换节点:
data Direction = L | R deriving (Show, Eq, Ord)
type Directions = [Direction]
changeNode :: Directions -> Tree Char -> Tree Char
changeNode (L : ds) (Node x l r) = Node x (changeNode ds l) r
changeNode (R : ds) (Node x l r) = Node x l (changeNode ds r)
changeNode [] (Node _ l …Run Code Online (Sandbox Code Playgroud) 我想通过以下函数传递State monad:
e1 :: Int -> (Bool, Int)
e1 el
| el > 100 = (True, el)
| otherwise = (False, 0)
e2 :: Int -> (Bool, Int)
e2 el
| el > 200 = (True, el)
| otherwise = (False, 0)
e3 :: Int -> (Bool, Int)
e3 el
| el > 300 = (True, el)
| otherwise == (False, 0)
implementor :: State Bool Int
implementor = state e1 ...
main = do
print $ runState implementor 10 …Run Code Online (Sandbox Code Playgroud) 我有以下简单的数组:
my_array = [1, 11, 44, 4]
Run Code Online (Sandbox Code Playgroud)
我想生成一个由这些元素之间的差异组成的新数组,所以它将是:
diff_array = [10, 33, 40]
Run Code Online (Sandbox Code Playgroud)
解决这个问题的最佳方法是什么?
超级基本问题 - 但我似乎无法得到一个明确的答案.以下函数将无法编译:
randomfunc :: a -> a -> b
randomfunc e1 e2
| e1 > 2 && e2 > 2 = "Both greater"
| otherwise = "Not both greater"
main = do
let x = randomfunc 2 1
putStrLn $ show x
Run Code Online (Sandbox Code Playgroud)
我很困惑为什么这不起作用.两个参数都是'a'(Ints)类型,返回参数是'b'(String)类型?
错误:
"Couldn't match expected type ‘b’ with actual type ‘[Char]’"
Run Code Online (Sandbox Code Playgroud) 以下代码将无法编译:
data Outcome a = Fail | Pass a deriving (Show, Eq, Ord, Functor)
myList = [Pass 33, Pass 12, Fail, Pass 45]
main = do
print $ fmap (+1) myList
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么这不起作用,因为我已经成功地将相同的方法应用于我的二叉树类型'data Tree a = Empty | 节点a(树a)(树a)'?
考虑以下内容:
class Test m a where
t :: Int -> m a
instance Test [] Int where
t i = [i]
instance Test Maybe Int where
t i | i == 0 = Nothing
| otherwise = Just i
main = do
print $ t (22 :: Int) --Error!
Run Code Online (Sandbox Code Playgroud)
抛出以下错误:
Ambiguous type variables ‘m0’, ‘a0’ arising from a use of ‘print’
prevents the constraint ‘(Show (m0 a0))’ from being solved.
Run Code Online (Sandbox Code Playgroud)
这是因为编译器无法识别m a要使用的实例.我该如何明确说明这一点?
haskell ×8
functor ×2
javascript ×2
monads ×2
types ×2
applicative ×1
arrays ×1
gadt ×1
hash ×1
state-monad ×1
tree ×1