haskell

Why can't I import Control.Monad.Writer while I have mtl-2.2.2 installed?

你。 提交于 2021-02-05 10:49:47
问题 I am trying to import Writer monad which is defined in mtl-2.2.2 package which I have installed as I checked with ghc-pkg list . When I tried to do import Control.Monad.Writer in ghci it is giving me an error. I don't understand why? Prelude> import Control.Monad.Writer <no location info>: error: Could not find module ‘Control.Monad.Writer’ Perhaps you meant Control.Monad.Fail (from base-4.11.1.0) Control.Monad.Fix (from base-4.11.1.0) Control.Monad.Zip (from base-4.11.1.0) 来源: https:/

Searching through list of tuples

谁说胖子不能爱 提交于 2021-02-05 10:43:05
问题 I'm trying to write a function that accepts a string and a list of tuple pairs. I want to search through the list of tuples, and if the first value in the tuple matches the input string, I want to return the second value in the pair. I believe it functions similar to the lookup function, but I'm not sure how to implement it. Here is my thinking so far: search :: a -> [(a,b)] -> Maybe b search a (x:xs) = if a == first value in x, return second value in x -- If a is not in the list of tuples,

GHCI unable to recognize packages installed with Cabal on windows 10

妖精的绣舞 提交于 2021-02-05 10:40:14
问题 I am a beginner with Haskell and for an assignment I needed to install System.Random. So I installed the package using Cabal through command line (originally with cabal install random and then with cabal install --lib random ), however, when I run GHCI it doesn't recognize that the package is installed. When I run it I get this error: Could not find module `System.Random' Use -v to see a list of the files searched for. | 3 | import System.Random | ^^^^^^^^^^^^^^^^^^^^ Failed, no modules

What causes a type error like this in Haskell?

自古美人都是妖i 提交于 2021-02-05 10:01:48
问题 I'm playing with Haskell to evaluate simple limits with tables of values. I have the following function defined: f :: (Integral a) => a -> a f x = div 1 $ subtract 6 x and in GHCI, I let leftSide = [5.90, 5.91..5.99] and let rightSide = [6.10,6.09..6.01] , then: GHCI> map f leftSide Which barfs up this error: <interactive>:50:5 No instance for (Integral Double) arising from a use of `f' Possible fix: add an instance declaration for (Integral Double) In the first argument of `map', namely `f'

Haskell: Non-exhaustive patterns in function (simple functions) [duplicate]

天涯浪子 提交于 2021-02-05 09:37:39
问题 This question already has answers here : Better exception for non-exhaustive patterns in case (2 answers) Closed 4 years ago . I am confused as to why the 1st and 3rd versions of this functions give this error whereas the second definition works fine. -- head and tail third :: [a] -> a third [a] = head (tail (tail[a])) -- Pattern matching third2 :: [a] -> a third2 (_:_:x:_) = x -- List indexing third3 :: [a] -> a third3 [a] = [a]!!2 Thanks in advance 回答1: That is odd that the second one does

How do I change a certain value in a matrix in Haskell?

烂漫一生 提交于 2021-02-05 08:48:47
问题 I'm very new to Haskell and haven't fully understood how it works yet. In the following method I would like to change a certain value in a matrix or as it is realized in Haskell, a list of lists. setEntry :: [[Int]] -> Int -> Int -> Int -> [[Int]] setEntry x i j aij = This is my method so far. I know it's not much but I really don't know how to continue. The plan is to give it a matrix and then change the value that can be found in the ith line and the jth column to aij. I'd be very grateful

How can I compute a histogram in Haskell?

回眸只為那壹抹淺笑 提交于 2021-02-05 08:15:28
问题 I found Statistics.Sample.Histogram , but I can't seem to use it. If I want to be able to bin a list into four categories, I expect to be able to do something like this: import Statistics.Sample.Histogram histogram 4 [1, 2, 9, 9, 9, 9, 10, 11, 20] But it gives me the error "non type-variable argument in the constraint," which I don't understand at all. What am I doing wrong? 回答1: histogram takes a Vector of values, not a list. You can use Data.Vector 's fromList function to convert your list

Why is there infinite recursion when I apply a function with the argument n-1 (no parentheses), inside the application of another function?

拈花ヽ惹草 提交于 2021-02-05 06:53:21
问题 Here I can pass to function f the argument 7-1 , without parentheses. Prelude> f = (+1) Prelude> f 7-1 7 1. Why is the following an infinite recursion? Prelude> addRec :: (Eq a, Num a) => a -> a; addRec 0 = 0; addRec n = n + addRec n-1; Prelude> addRec 5 2. I can fix it by either adding parentheses to n-1 Prelude> addRec :: (Eq a, Num a) => a -> a; addRec 0 = 0; addRec n = n + addRec (n-1); 3. Or by using the $ operator with parentheses on the whole addRec recursion term: Prelude> addRec ::

Why is there infinite recursion when I apply a function with the argument n-1 (no parentheses), inside the application of another function?

╄→尐↘猪︶ㄣ 提交于 2021-02-05 06:52:54
问题 Here I can pass to function f the argument 7-1 , without parentheses. Prelude> f = (+1) Prelude> f 7-1 7 1. Why is the following an infinite recursion? Prelude> addRec :: (Eq a, Num a) => a -> a; addRec 0 = 0; addRec n = n + addRec n-1; Prelude> addRec 5 2. I can fix it by either adding parentheses to n-1 Prelude> addRec :: (Eq a, Num a) => a -> a; addRec 0 = 0; addRec n = n + addRec (n-1); 3. Or by using the $ operator with parentheses on the whole addRec recursion term: Prelude> addRec ::

How to store value of function call to a variable

久未见 提交于 2021-02-05 06:43:45
问题 I have this function where I need to check if the gdc of the numbers [1..n] and n is == 1 and do some calculations then. So I am stuck because I can't find a way to store the initial value of n to a variable. For example, if I call the function with the number 7 its a recursion so n becomes 6 then 5 etc so I can't gdc properly; for example 1-7 then 2 - 7 then 3 -7 . Do you know how I can store the value of n to a variable ? myproduct :: Integer->Integer myproduct 0 = 1 myproduct n |gcd n (n