我有以下Haskell表达式:
3 : [40] ++ [50] ++ 5 : [60]
Run Code Online (Sandbox Code Playgroud)
我想知道如何评估此表达式。哪个运算符具有更高的优先级,:或者++?我认为表达式的结果是[3,40,50,5,60],但是我通过以下方式做到了:
3 : [40] ++ [50] ++ 5 : [60]
3 : [40] ++ [50] ++ [5,60]
3 : [40] ++ [50,5,60]
3: [40,50,5,60]
[3,40,50,5,60]
Run Code Online (Sandbox Code Playgroud)
以上是评估表达式的正确方法吗?任何见解都表示赞赏。
我有以下定理:(A?(B?C))?((A?B)?C),并且我试图确定其计算解释是什么。我有以下选择:
(1)将已咖喱函数转换为未咖喱函数的函数。
(2)将未修改的函数转换为已修改的函数的函数。
(3)该函数创建两个给定的A值和B值的元组。
(4)此逻辑公式没有计算解释。
我认为(1)或(2)都不正确,因为在Haskell中,所有函数都被视为咖喱。我认为选项(3)是正确的,因为(A?B)是转换为类型时的元组(A,B)。但是,我不确定我的推理是否正确。是否可以有一个curried函数,其中您使用前两个参数A和B,然后以某种方式将它们转换为元组,然后返回值C?任何见解都表示赞赏。
我有以下功能:
parse :: String -> Maybe Token
Run Code Online (Sandbox Code Playgroud)
我正在尝试实现以下功能:
maketokenlist :: String -> Maybe [Token]
Run Code Online (Sandbox Code Playgroud)
如果存在无法解析的标记,则该函数返回Nothing(即,如果该标记不是整数或算术运算符,则parse返回Nothing),否则它将返回一个标记列表。
由于Maybe是Monad类型类的实例,因此我采用以下方法:
maketokenlist str = return (words str) >>= parse
Run Code Online (Sandbox Code Playgroud)
我将字符串转换为单个标记的列表(例如“ 2 3 +”变为[“ 2”,“ 3”,“ +”],然后将解析函数映射到列表中的每个字符串上。
由于列表的Monad实例定义为:
instance Monad [] where
return x = [x]
xs >>= f = concat (map f xs)
fail _ = []
Run Code Online (Sandbox Code Playgroud)
但是,假设我有字符串列表[2,3,“ +”,“ a”],并使用>> =在每个元素上映射解析之后,我得到了[Just 2,Just 3,Just(+),Nothing] ,因为无法解析“ a”。有没有办法仅使用>> =运算符使maketokenlist函数不返回任何内容?任何见解都表示赞赏。
我有以下用于操纵机器人的API的代码:
data Direction = Left | Right
forward :: IO ()
blocked :: IO Bool
turn :: Direction -> IO ()
Run Code Online (Sandbox Code Playgroud)
我正在尝试理解两个程序,它们将使机器人向前移动,除非它被障碍物挡住,在这种情况下,机器人应向正确的方向旋转。
但是,我不确定以下两个程序之间有什么区别:
-- program 1
robot = do
detected <- blocked
if detected
then turn Right
else forward
robot
-- program 2
robot = do
detected <- blocked
if detected
then turn Right
robot
else forward
robot
Run Code Online (Sandbox Code Playgroud)
该行将detected <- blocked布尔值从IO中取出。如果条件if detected为真,则机器人向右转,否则机器人向前移动。在程序1中,向右或向前移动机器人后,将再次调用功能机器人。在程序2中,在右转或前进后直接调用功能机器人。
我不确定在if-else语句之后(在程序1中)调用机器人与在程序2 中的thenand else案例中调用机器人之间有什么区别?我是否正确地说这两个程序是等效的?任何见解都表示赞赏。
monads haskell functional-programming equivalence do-notation
我有以下Haskell表达式:
a = getLine >>= putStrLn . filter isDigit >> a
Run Code Online (Sandbox Code Playgroud)
我无法理解上述表达式的工作原理。我知道该>>=函数需要一个monadic值和一个函数(该函数需要一个正常值并返回monadic值),然后返回monadic值。
我知道,getLine并putStrLn具有以下类型声明:
getLine :: IO String
putStrLn :: String -> IO ()
Run Code Online (Sandbox Code Playgroud)
所以下面的表达式部分:
a = getLine >>= putStrLn . filter isDigit
Run Code Online (Sandbox Code Playgroud)
将返回一个IO ()。但是,该函数>>将获取第一个Monadic值和第二个Monadic值并返回第二个Monadic值。
给定原始表达式,传递给的第一个参数>>将为type IO String。第二个参数是a。
我的问题是,的类型是什么a,上述表达式如何工作以连续接收用户输入并仅将输入的数字部分打印回屏幕?任何见解都表示赞赏。
我正在“为哈萨克斯坦学到伟大的东西!”一书中学习有关State monad的信息。由Miran Lipovaca撰写。对于以下monad实例:
instance Monad (State s) where
return x = State $ \s -> (x,s)
(State h) >>= f = State $ \s -> let (a, newState) = h s
(Stage g) = f a
in g newState
Run Code Online (Sandbox Code Playgroud)
我在理解>>=函数定义时遇到麻烦。我不确定h状态计算(即采用状态并返回具有更新状态的结果的函数)还是状态。我猜想它必须是有状态的计算,因为它被应用于slambda函数中的类型状态以产生结果(a, newState)。
但是从状态sa的类型声明:
newtype State s a = State { runState :: s -> (a,s) }
Run Code Online (Sandbox Code Playgroud)
状态是类型s,结果是类型a。所以对于单子的实例是s在instance Monad (State s) where状态的类型或它实际上是有状态的计算?任何见解都表示赞赏。
我正在尝试确定是否针对以下类型:((a -> c) -> c) -> a可以使用该类型作为函数签名来编写总终止函数?我知道,为了使一个函数完整,必须为其输入的所有可能值定义一个函数。但是,我不确定函数终止到底意味着什么。为了使函数终止,它是否必须返回一个值(例如,不进入无限循环)?
此外,我可以使用哪些方法来证明可以使用编写总的终止函数((a -> c) -> c) -> a?任何见解都表示赞赏。
I have the following table called Involves:
match | team
10 | A
10 | B
20 | B
20 | C
30 | C
30 | A
40 | D
40 | C
50 | A
50 | B
Run Code Online (Sandbox Code Playgroud)
The values in the column 'match' refer to the unique id of the match and the values in the column 'team' refer to the unique id of the team.
I am trying to write a query that will output the …
我有以下代码,概述了布尔和算术表达式的语言:
data Exp a where
Plus :: Exp Int -> Exp Int -> Exp Int
Const :: (Show a) => a -> Exp a
Not :: Exp Bool -> Exp Bool
And :: Exp Bool -> Exp Bool -> Exp Bool
Greater :: Exp Int -> Exp Int -> Exp Bool
Run Code Online (Sandbox Code Playgroud)
以下是仅评估算术表达式的函数的代码:
evalA (Plus a b) = evalA a + evalA b
evalA (Const a) = a
Run Code Online (Sandbox Code Playgroud)
我试图弄清楚应该给哪种类型的签名以evalA使其完整。但是,我不知道对一个类型签名进行总计意味着什么。任何见解都表示赞赏。
我正在从“学习Haskell为伟大!”一书中学习单子。由Miran Lipovaca撰写。我试图理解单子的结合律。本质上,法律规定,当您使用拥有一元函数应用程序链时>>=,如何嵌套它们无关紧要。
以下代码使人们能够将type函数的结果传递给type a -> m b函数b -> m c:
(<=<) :: (Monad m) => (b -> m c) -> (a -> m b) -> (a -> m c)
f <=< g = (\x -> g x >>= f)
Run Code Online (Sandbox Code Playgroud)
但是,对于以下示例:
ghci> let f x = [x, -x]
ghci> let g x = [x*3, x*2]
ghci> let h = f <=< g
ghci> h 3
[9, -9, 6, -6]
Run Code Online (Sandbox Code Playgroud)
是f x和g x两个功能?看来它们是具有不同x值而不是函数的列表。该行在 …
haskell ×9
monads ×4
currying ×1
do-notation ×1
equivalence ×1
io-monad ×1
kleisli ×1
list ×1
postgresql ×1
sql ×1