helper(a, b, c)
's syntax sugar in this case?
//haskell
replaceWithP = const 'p'
lmls = [Just ["Ha", "Ha"], Nothing, Just []]
(fmap . fmap . fmap . fmap) replaceWithP lmls
-- [Just ["pp","pp"],Nothing,Just []]
//scala
val lmls = List(Some(List("Ha", "Ha"), None))
lmls.map(_.map(_.map(_.map(_ => 'p'))))
runGet
, what I get is [\nul, \nul, \nul, \nul, \nul, \nul, \nul, 3, 1, 2, 3]
faculty :: Int -> Int
faculty 0 = 1
faculty n = n * faculty (n-1)
cos :: Double -> Double
cos x = sum [cos| k <- [0..],let cos = (-1) * (x^(2*k) `div` faculty (2*k)) , abs (cos) > 0.001]
-- Tic-tac-toe example from chapter 11 of Programming in Haskell,
-- Graham Hutton, Cambridge University Press, 2016.
-- Basic declarations
import Data.Char
import Data.List
import System.IO
size :: Int
size = 3
type Grid = [[Player]]
data Player = O | B | X
deriving (Eq, Ord, Show)
grid :: Grid
grid = [[B,O,O],[O,X,O],[X,X,X]]
next :: Player -> Player
next O = X
next B = B
next X = O
-- Grid utilities
empty :: Grid
empty = replicate size (replicate size B)
full :: Grid -> Bool
full = all (/= B) . concat
turn :: Grid -> Player
turn g = if os <= xs then O else X
where
os = length (filter (== O) ps)
xs = length (filter (== X) ps)
ps = concat g
wins :: Player -> Grid -> Bool
wins p g = any line (rows ++ cols ++ dias)
where
line = all (== p)
rows = g
cols = transpose g
dias = [diag g, diag (map reverse g)]
diag :: Grid -> [Player]
diag g = [g !! n !! n | n <- [0..size-1]]
won :: Grid -> Bool
won g = wins O g || wins X g
-- Displaying a grid
putGrid :: Grid -> IO ()
putGrid =
putStrLn . unlines . concat . interleave bar . map showRow
where bar = [replicate ((size*4)-1) '-']
showRow :: [Player] -> [String]
showRow = beside . interleave bar . map showPlayer
where
beside = foldr1 (zipWith (++))
bar = replicate 3 "|"
showPlayer :: Player -> [String]
showPlayer O = [" ", " O ", " "]
showPlayer B = [" ", " ", " "]
showPlayer X = [" ", " X ", " "]
interleave :: a -> [a] -> [a]
interleave x [] = []
interleave x [y] = [y]
interleave x (y:ys) = y : x : interleave x ys
row= [O,B,B]
*Main> showRow row
[" | | "," O | | "," | | "]
*Main> (interleave bar) . (map showPlayer row)
<interactive>:68:21: error:
• Couldn't match expected type ‘a -> [[[Char]]]’
with actual type ‘[[String]]’
• Possible cause: ‘map’ is applied to too many arguments
In the second argument of ‘(.)’, namely ‘(map showPlayer row)’
In the expression: (interleave bar) . (map showPlayer row)
In an equation for ‘it’:
it = (interleave bar) . (map showPlayer row)
• Relevant bindings include
it :: a -> [[[Char]]] (bound at <interactive>:68:1)
A full version of the question is here:
Hey people
Can someone explain why I am getting this
Expected type: IO Main.Result
Actual type: t0 Maybe Main.Result
What I am doing is the following
fetchVods :: [Customer] -> IO [Vod]
fetchVods customers = undefined
handleCustomerVods :: Customer -> Main.Result
handleCustomerVods = undefined
checkForFail :: Main.Result -> Maybe Main.Result
checkForFail r = case r of { OK -> Nothing;
FAIL -> Just FAIL
}
main = do
args <- execParser opts
result <- lift $ generateVodCsvForCustomers args
display args
where
opts = info (set_config <**> helper)
( fullDesc
<> progDesc "Generate Vod CSV customers"
<> header "CSV Generator"
)
generateVodCsvForCustomers config = firstJust (checkForFail) $ (fmap handleCustomerVods $ customers config)
Does this make sense? Why is t0 not being implicitly mapped as IO, since ints a context of IO ?
Assuming that lift
is not to be used, what is the alternative?