haskell

Re-write 'map intToDigit' with a fold…

三世轮回 提交于 2019-12-31 05:09:36
问题 So this one seems like it should be super-simple... but I'm not sure where to stick the 'fold' in (obviously you could fold either way)... It says "write a function ( intToString :: [Int] -> [Char] ) using a fold, that mimics this map: map intToDigit [5,2,8,3,4] == "52834" And then says, "For the conversion, use intToDigit :: Int -> Char from Data.Char ." I'm not entirely sure I get the point... but yet it doesn't seem like it should be that hard -- you're just reading in the list (folding it

Creating 2 dimensional list matrix

廉价感情. 提交于 2019-12-31 04:54:04
问题 How do you create a list-based matrix of 1 's with given row and column counts? For example, like: row=3,column=4 -> [[1,1,1,1],[1,1,1,1],[1,1,1,1]] 回答1: You can do this with replicate : onesMatrix rows cols = replicate rows (replicate cols 1) 回答2: Here's an alternative method using list comprehension. Let's have a look at the basics: Prelude> [ 2*x | x <- [1..4] ] [2,4,6,8] So that gives you one number for each element in the list [1..4] . Why not, instead of doubling x , just have a 1 :

Capturing Persistent Relations in a Form

≯℡__Kan透↙ 提交于 2019-12-31 04:43:12
问题 I have defined a one-to-many relationship in Persistent but could not figure out how to create a form that can take one of the foreign keys as input. Simplifying my use case to something like this: Person name String Car personId PersonId name Text type Text Now when I try to generate a Form for Car, what should be the field type for personId? I tried something like this but get an error: entryForm :: Maybe Car -> Form Car entryForm car = renderDivs $ Car <$> areq personIdField "Person"

Haskell - Maybe Either

你说的曾经没有我的故事 提交于 2019-12-31 04:14:07
问题 -- | Convert a 'Maybe a' to an equivalent 'Either () a'. Should be inverse -- to 'eitherUnitToMaybe'. maybeToEitherUnit :: Maybe a -> Either () a maybeToEitherUnit a = error "Not yet implemented: maybeToEitherUnit" -- | Convert a 'Either () a' to an equivalent 'Maybe a'. Should be inverse -- to 'maybeToEitherUnit'. eitherUnitToMaybe :: Either () a -> Maybe a eitherUnitToMaybe = error "Not yet implemented: eitherUnitToMaybe" -- | Convert a pair of a 'Bool' and an 'a' to 'Either a a'. Should be

Haskell - filter string list based on some conditions

安稳与你 提交于 2019-12-31 03:38:05
问题 I am new in this comunity. I learn Haskell and have difficulties with Haskell-coding. I hope you can help me. I searched here and in Google, without any success. My problem ist as fowllows: I want to write a function which takes a list as parameter like this: myStringListFilter :: [String] -> [String] process the following steps: Remove the first letter myStringListFilter myList = map tail strListe myList Filter every element in the list which begins with "u" or "U". myStringListFilter myList

Arrays with rigid variable

吃可爱长大的小学妹 提交于 2019-12-31 03:24:06
问题 Well, I was doing a problem in which function I was using had a rigid variable. I had an idea of using arrays for that problem. So I thought of using arrays with same rigid variable as of the function I was creating, but I have no idea how to make an array with a rigid variable. I tried the following thing but with no effect: rearrange :: [Int] -> [a] -> [a] rearrange l la = elems (f 1 posarr) where b = length l listarr :: Array Int Int listarr = listArray (1,b) l arra :: Array Int c arra =

GHC: Display of unicode characters

て烟熏妆下的殇ゞ 提交于 2019-12-31 03:23:07
问题 Further to my first question on the management of the unicode characters in the production of .exe file, this is also a bug in GHC? > print "Frère" "Fr\233re" 回答1: print x is equivalent to putStrLn (show x) , where show converts a type of the Show class to a string representation. In your case, x already has the String type. One might think that the String implementation of show would simply return its argument unchanged, but actually it transforms it into an ASCII string literal token with

() in Function Variable and Application

老子叫甜甜 提交于 2019-12-31 03:19:10
问题 The Types and Functions lecture presents the function: f44 :: () -> Integer f44 () = 44 I typed the following: ghci> let f () = 5 ghci> f () 5 But, I'm confused by the () in let f () . Typically, as a beginner, I've seen an immutable variable following the function name, i.e. f . What is the name of () when it's listed after let f ... ? How about when it's used in the function application, f () ? 回答1: "()" is usually pronounced "unit". In Haskell, it is both the name of a type, as seen in f44

MonadError section in “All about monads”

大城市里の小女人 提交于 2019-12-31 03:17:10
问题 I'm now really confused about the Error monad in which "All about monads" describes. It claims the definition of Error monad as class (Monad m) => Monaderror e m | m -> e where throwError :: e -> m a catchError :: m a -> (e -> m a) -> m a And one of the instance is Either e. instance MonadError (Either e) where throwError = Left (Left e) `catchError` handler = handler e a `catchError` _ = a Here is what I don't understand. The MonadError class take two type parameters, and (Either e) takes

Splitting list into n-tuples [duplicate]

南笙酒味 提交于 2019-12-31 03:12:15
问题 This question already has answers here : Grouping a list into lists of n elements in Haskell (4 answers) Closed 2 years ago . How can I split a list into list of tuples/lists of specified length? splitBy :: Int -> [a] -> [[a]] splitBy 2 "asdfgh" should return ["as", "df", "gh"] 回答1: splitEvery usually gets the nod for this job. 回答2: Searching Hoogle for Int -> [a] -> [[a]] yields chunksOf, which may be of use. 回答3: One way of doing it: splitBy :: Int -> [a] -> [[a]] splitBy _ [] = [] splitBy