我正在尝试创建一个验证输入的函数String -> Maybe Int.我检查输入字符串是否是数字然后检查该数字是否在一个范围内.到目前为止我有
validateNumber :: String -> Maybe Int
validateNumber n = go $ (readMaybe::String -> Maybe Int) n
where
go (Just a) = inRange a
go Nothing = Nothing
inRange :: Int -> Maybe Int
inRange n
| n > 0 = Just n
| otherwise = Nothing
Run Code Online (Sandbox Code Playgroud)
这感觉就像糟糕的代码.怎么写呢?
此外,如果我试图循环一个函数,如果它返回Nothing,最好的方法是什么:
所以要循环这个main函数,我正在做:
case v of
Nothing -> main
Just x -> {do something}
Run Code Online (Sandbox Code Playgroud) 我遇到一个问题,即使该字段没有值,input类型字段仍显示当前日期。date我努力了:
... placeholder="dd/mm/yyyy">
但问题仍然存在。有没有其他方法可以在不更改输入字段类型的情况下设置此占位符?
如果我有,JSON并且我尝试FromJSON自动派生实例Generics,我遇到问题,id存在于多个地方JSON.
有没有办法让我只覆盖id部分或者我必须编写整个实例才能更改这些特定的条目?在JSON实际上有更多的领域,但我最左侧的它在这个例子.因此,写出整个FromJSON实例实际上是相当繁琐的.
JSON:
{
"response": [
{
"id": 1,
"brandId": 1,
"productTypeId": 1,
"identity": {
"sku": "x",
"barcode": "Ax"
},
"stock": {
"stockTracked": false,
"weight": {
"magnitude": 0
},
"dimensions": {
"length": 0,
"height": 0,
"width": 0,
"volume": 0
}
},
"financialDetails": {
"taxable": false,
"taxCode": {
"id": 1,
"code": "x"
}
},
... etc
]
}
Run Code Online (Sandbox Code Playgroud)
代码到目前为止:
data Response = Response
{ response …Run Code Online (Sandbox Code Playgroud) 我试图在2D平面上跟踪对象的移动,该平面已被给出"向前,向左或向右"命令列表.
到目前为止,我有一些函数可以接收对象状态的组件(方向,位置和移动),并在完成所有移动或沿途传递所有位置后返回最终状态.
对象的状态是在形式中Sstate (Dir dx dy) (Pos px py) [m],移动包括递归地应用移动列表的头部以生成新状态
即 Sstate (Dir 1 0) (Pos 0 0) "fff" -> Sstate (Dir 1 0) (Pos 0 1) "ff"
type Mov = Char
data Pos = Pos Int Int
deriving (Show, Eq)
data Dir = Dir Int Int
deriving (Show, Eq)
data Sstate = Sstate Dir Pos [Mov]
deriving (Show, Eq)
move :: Sstate -> Sstate
move (Sstate (Dir dx dy) (Pos px py) [] ) = Sstate (Dir …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Laravel 应用程序中编写一个函数来重命名 S3 驱动器上的文件。
我努力了:
if(Storage::disk('s3')->exists($old_path)) {
Storage::disk('s3')->move($old_path, $new_path);
}
Run Code Online (Sandbox Code Playgroud)
但它没有做任何事情。
dd(Storage::disk('s3')->exists($old_path))
Run Code Online (Sandbox Code Playgroud)
回报True
还
Storage::disk('s3')->put($full_path, file_get_contents($file));
Storage::disk('s3')->get($full_path);
Run Code Online (Sandbox Code Playgroud)
两者都工作正常!
是否可以在向量上使用列表样式模式匹配?
即
import qualified Data.Vector as V
f :: V.Vector a -> a
f (x:xs) = x
Run Code Online (Sandbox Code Playgroud)
给出错误
我正在尝试将数据类型编码为JSON:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
import Data.Aeson
data Trend = Trend
{ period :: String
, africa :: String
, americas :: String
, asia :: String
} deriving Show
instance ToJSON Trend where
toJSON Trend{..} =
object [ "Period" .= period
, "Africa" .= africa
, "Americas" .= americas
, "Asia" .= asia
]
test = Trend {period = "2013", africa = "1", americas = "2", asia = "3"}
Run Code Online (Sandbox Code Playgroud)
这使:
?: encode test
?: "{\"Asia\":\"3\",\"Period\":\"2013\",\"Africa\":\"1\",\"Americas\":\"2\"}" …Run Code Online (Sandbox Code Playgroud) 如果我有一个随机播放“纸牌”的函数,如何使用State Monad来遍历定义的随机播放数量,然后返回结果?
例如,我有以下功能,可以先对卡片组进行1次洗牌,然后返回特定的卡片:
step :: State [String] String
step = do
modify shuffle
deck <- get
pure $ bestCard deck
Run Code Online (Sandbox Code Playgroud)
我想做的是在返回值之前对状态更改进行5次迭代。
我试过的是:
steps :: Int -> State [String] String
steps n = case n of
0 -> do
deck <- get
pure $ bestCard deck
_ -> do
modify shuffle
steps (n - 1)
Run Code Online (Sandbox Code Playgroud)
但这看起来远不是正确的方法,即使它可行。
注意 我知道无需使用即可完成此操作,State Monad但我尝试使用此示例来学习使用方法State。
编辑:
感谢@Koterpillar,我可以replicateM用来获取想要的东西。
evalState (replicateM n $ modify shuffle >> get >>= pure . bestCard)
我正在尝试学习如何使用ST Monad. 我想知道是否可以在采取行动时打印值。例如,使用Fibonacci以下函数ST Monad:
fibST :: Integer -> Integer
fibST n
| n < 2 = n
| otherwise =
runST $ do
a <- newSTRef 0
b <- newSTRef 1
go n a b
where
go 0 a _ = readSTRef a
go n a b = do
a' <- readSTRef a
b' <- readSTRef b
writeSTRef a b'
writeSTRef b (a' + b')
-- print "SOME INFORMATION HERE" <---
go (n - …Run Code Online (Sandbox Code Playgroud) 我正在查看一些 JAVA 代码,我想知道如何将其转换为 Haskell。
IntStream.range(0, cookedWords.length).parallel().forEach((int i) -> {
int A = cookedWords[i];
for (int j = i + 1; j < cookedWords.length; ++j) {
int B = cookedWords[j];
if ((A & B) != 0) continue;
int AB = A | B;
for (int k = j + 1; k < cookedWords.length; ++k) {
int C = cookedWords[k];
if ((AB & C) != 0) continue;
int ABC = AB | C;
for (int l = k + 1; l < …Run Code Online (Sandbox Code Playgroud)