haskell

Type error in string shuffle function

帅比萌擦擦* 提交于 2020-01-25 07:16:01
问题 I have tried to create my own string shuffle function: import System.Random -- usage case: my_shuffle "something" "" my_shuffle :: [Char] -> [Char] -> [Char] my_shuffle [] result = result my_shuffle s result = do pos <- randomRIO (1, length s) my_shuffle (remove_char pos) (result ++ (get_char pos)) get_char :: [Char] -> Int -> Char get_char s pos = s !! (pos - 1) remove_char :: [Char] -> Int -> [Char] remove_char s pos = take (pos - 1) s ++ drop pos s It returns the error message:

How to make my function move through two lists at once?

允我心安 提交于 2020-01-25 06:51:29
问题 I'm trying to make a function that will take a list of lists of Int s as an input, and adds +1 every time it runs into a number bigger or equal to 10. I added -20 on each side so xc can start at 0. Example what should happen after the function runs into first '10': [[-20,-20, 0, 0, 0, 0, 0, 0, 0,-20,-20], [-20,-20, 0,10, 1, 0, 0, 0, 0,-20,-20], [-20,-20, 1, 1, 1, 0,10, 0, 0,-20,-20], [-20,-20, 0, 0, 0,10, 0, 0, 0,-20,-20], [-20,-20, 0, 0, 0, 0, 0, 0,10,-20,-20], [-20,-20,10,10,10, 0, 0, 0, 0,

Filter for first matching monadic action (without evaluating all actions)?

久未见 提交于 2020-01-25 06:40:28
问题 Is there a standard / optimized implementation of the following function that I'm writing (probably unnecessarily): filterFirstM :: (Monad m, Foldable t) => (a -> Bool) -> t m a -> m a filterFirstM predicate actions = foldlM fn Nothing actions where fn memo action = case memo of Just _ -> pure memo Nothing -> do x <- action pure $ if predicate x then (Just x) else Nothing Sample usage: filterFirstM (== 1) [pure 0 :: IO Int, pure 1, error "not evaluated"] == (pure 1) 来源: https://stackoverflow

Filter for first matching monadic action (without evaluating all actions)?

≡放荡痞女 提交于 2020-01-25 06:40:08
问题 Is there a standard / optimized implementation of the following function that I'm writing (probably unnecessarily): filterFirstM :: (Monad m, Foldable t) => (a -> Bool) -> t m a -> m a filterFirstM predicate actions = foldlM fn Nothing actions where fn memo action = case memo of Just _ -> pure memo Nothing -> do x <- action pure $ if predicate x then (Just x) else Nothing Sample usage: filterFirstM (== 1) [pure 0 :: IO Int, pure 1, error "not evaluated"] == (pure 1) 来源: https://stackoverflow

Haskell: Get block for Sudoku solver

点点圈 提交于 2020-01-25 04:42:08
问题 I am trying to build a Sudoku solver for a project I am working on. I have a 9x9 grid with each position numbered 0..80, left to right, top to bottom e.g.: 0 1 2 3 4 5 6 7 8 9 10 ... I am trying to return a list of Int's that represent the 3x3 grid that a position falls in. For example for position 1, which is in grid (0,0) it would return [0,1,2,9,10,11,18,19,20] and for position 8, which is in grid (0,2) it would return [6,7,8,15,16,17,24,25,26]. I have written a function which returns the

Map function applied to specific elements of the list

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-25 03:10:09
问题 I have a function: mapAtOriginal :: (a -> a) -> [Int] -> [a] -> [a] mapAtOriginal f is xs = helper 0 is xs where helper _ [] xs = xs helper c (i:is) (x:xs) | c < i = x : helper (c+1) (i:is) xs | otherwise = f x : helper (c+1) is xs It works like this: mapAtOriginal (*2) [0,3] [1,2,3,4] -- == [2,2,3,8] So I want to rewrite it but using a map function. I understand that map applies to every element of the list, however, I need it applied only for specific indices. How can I do it? 回答1: map

Regular Expression on Yesod type Text

家住魔仙堡 提交于 2020-01-25 00:59:08
问题 Currently, I am changing my Text to String then using Text.Regex.Posix to do my matching. Is there a idiomatic and efficient way to do regular expression in Yesod? 回答1: Have you taken a look at the regex-tdfa-text package? It provides backend support for using the regex-base library with Text strings. 来源: https://stackoverflow.com/questions/31160216/regular-expression-on-yesod-type-text

Haskell: Assigning unique char to matrix values if x > 0

℡╲_俬逩灬. 提交于 2020-01-25 00:20:14
问题 So my goal for the program is for it to receive an Int matrix for input, and program converts all numbers > 0 to a unique sequential char, while 0's convert into a '_' (doesn't matter, just any character not in the sequence). eg. main> matrixGroupings [[0,2,1],[2,2,0],[[0,0,2]] [["_ab"],["cd_"],["__e"]] The best I've been able to achieve is [["_aa"],["aa_"],["__a"]] using: matrixGroupings xss = map (map (\x -> if x > 0 then 'a' else '_')) xss As far as I can tell, the issue I'm having is

Implementing the ruler function using `streamInterleave`

强颜欢笑 提交于 2020-01-24 22:14:24
问题 I am doing the homework of CIS 194. The problem is to implement the ruler function by using streamInterleave . The code looks like data Stream a = Cons a (Stream a) streamRepeat :: a -> Stream a streamRepeat x = Cons x (streamRepeat x) streamMap :: (a -> b) -> Stream a -> Stream b streamMap f (Cons x xs) = Cons (f x) (streamMap f xs) streamInterleave :: Stream a -> Stream a -> Stream a streamInterleave (Cons x xs) ys = Cons x (streamInterleave ys xs) ruler :: Stream Integer ruler =

Implementing the ruler function using `streamInterleave`

偶尔善良 提交于 2020-01-24 22:14:14
问题 I am doing the homework of CIS 194. The problem is to implement the ruler function by using streamInterleave . The code looks like data Stream a = Cons a (Stream a) streamRepeat :: a -> Stream a streamRepeat x = Cons x (streamRepeat x) streamMap :: (a -> b) -> Stream a -> Stream b streamMap f (Cons x xs) = Cons (f x) (streamMap f xs) streamInterleave :: Stream a -> Stream a -> Stream a streamInterleave (Cons x xs) ys = Cons x (streamInterleave ys xs) ruler :: Stream Integer ruler =