Very new to Haskell here. I'm not sure why this piece of case of syntax won't compile properly:
-- | Use column headers to determine offsets for each row in a table
splitHeader :: Alignment -> String -> [(String,Int)]
splitHeader a h = case a of
AlignLeft -> reverse ((foldr f b h) ("", 0) [] 2)
AlignRight -> reverse ((foldl f b h) ("", 0) [] 2)
where b (n', w') l' m' = (reverse n', w'):l'
f c b (n, w) l m
| m == 2 && c == ' ' = b (n, w+1) l 0
| m == 2 && c /= ' ' = b (c:n, w+1) l 1
| m == 1 && c == ' ' = b (n, w+1) l 0
| m == 1 && c /= ' ' = b (c:n, w+1) l 1
| m == 0 && c == ' ' = b (n, w+1) l 0
| m == 0 && c /= ' ' = b ([c], 1) ((reverse n, w):l) 1
Compile error:
Kata.hs:50:43: warning: [-Wdeferred-type-errors]
• Couldn't match type ‘([Char], Int)
-> [([Char], Int)] -> Integer -> [([Char], Int)]’
with ‘Char’
Expected type: (([Char], Int)
-> [([Char], Int)] -> Integer -> [([Char], Int)])
-> (([Char], Int) -> [([Char], Int)] -> Integer -> [([Char], Int)])
-> ([Char], Int)
-> [([Char], Int)]
-> Integer
-> [([Char], Int)]
Actual type: Char
-> (([Char], Int) -> [([Char], Int)] -> Integer -> [([Char], Int)])
-> ([Char], Int)
-> [([Char], Int)]
-> Integer
-> [([Char], Int)]
• In the first argument of ‘foldl’, namely ‘f’
In the first argument of ‘reverse’, namely
‘((foldl f b h) ("", 0) [] 2)’
In the expression: reverse ((foldl f b h) ("", 0) [] 2)
|
50 | AlignRight -> reverse ((foldl f b h) ("", 0) [] 2)
| ^
Kata.hs:50:47: warning: [-Wdeferred-type-errors]
• Couldn't match type ‘Char’
with ‘([Char], Int)
-> [([Char], Int)] -> Integer -> [([Char], Int)]’
Expected type: [([Char], Int)
-> [([Char], Int)] -> Integer -> [([Char], Int)]]
Actual type: String
• In the third argument of ‘foldl’, namely ‘h’
In the first argument of ‘reverse’, namely
‘((foldl f b h) ("", 0) [] 2)’
In the expression: reverse ((foldl f b h) ("", 0) [] 2)
|
50 | AlignRight -> reverse ((foldl f b h) ("", 0) [] 2)
| ^
Ok, one module loaded.
The types of foldr and foldl are slightly different:
foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b
But you are passing the same function f to both.
If you replace the argument f with flip f the types work out:
splitHeader :: Alignment -> String -> [(String,Int)]
splitHeader a h = case a of
AlignLeft -> reverse ((foldr f b h) ("", 0) [] 2)
AlignRight -> reverse ((foldl (flip f) b h) ("", 0) [] 2)
where b (n', w') l' m' = (reverse n', w'):l'
f c b (n, w) l m
| m == 2 && c == ' ' = b (n, w+1) l 0
| m == 2 && c /= ' ' = b (c:n, w+1) l 1
| m == 1 && c == ' ' = b (n, w+1) l 0
| m == 1 && c /= ' ' = b (c:n, w+1) l 1
| m == 0 && c == ' ' = b (n, w+1) l 0
| m == 0 && c /= ' ' = b ([c], 1) ((reverse n, w):l) 1
Related
module Luhn (isValid) where
import qualified Data.Char as C
isAsciiAlpha :: Char -> Bool
isAsciiAlpha = C.isAsciiLower || C.isAsciiUpper
isValid :: String -> Bool
isValid n
| any ((isAsciiAlpha || C.isSpace) . not) n = False
| otherwise = ys > 1 && sum xxs `mod` 10 == 0
where
xs = reverse [c | c <- n, isAsciiAlpha c]
ys = length xs
zs = zip xs (cycle [1, 2])
xxs = [convert x y | (x, y) <- zs]
convert :: Char -> Int -> Int
convert c mul =
do
let n = C.digitToInt c
case () of
_
| mul == 2 && (n > 4) -> n * mul - 9
| otherwise -> n * mul
I'm struggling with this line: any ((isAsciiAlpha || C.isSpace) . not) n = False. What I want is pretty obvious; find if any of the characters is something other than an ASCII alphabet or a space.
In spite of trying various syntaxes, I keep getting compilation error on this line, something like
• Couldn't match expected type ‘Bool’
with actual type ‘Char -> Bool’
You can not use (||) :: Bool -> Bool -> Bool on two functions: the parameters should be both Bools. What you can do is construct a function that maps a character c on isAsciiAlpha c || C.isSpace c, so \c -> isAsciiAlpha c || C.isSpace c, or you can use liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c with liftA2 (||) isAsciiAlpha C.isSpace. The not :: Bool -> Bool should also be applied on the result of the function, so:
import Control.Applicative(liftA2)
isAsciiAlpha :: Char -> Bool
isAsciiAlpha = liftA2 (||) C.isAsciiLower C.isAsciiUpper
isValid :: String -> Bool
isValid n
| any (not . liftA2 (||) isAsciiAlpha C.isSpace) n = False
| otherwise = ys > 1 && sum xxs `mod` 10 == 0
where -- …
You can also make use of (<||>) :: Applicative a => a Bool -> a Bool -> a Bool or its shortcircuitng version (||^) :: Monad m => m Bool -> m Bool -> m Bool of the protolude package.
I have written the following predicate (Lines 94-99)
diffFreqMatrix :: Fractional a => [[Rating a]] -> [a]
diffFreqMatrix (x:xs) = diffFreqMatrixH (x:xs) (matrixPairs (length x))
diffFreqMatrixH _ [] = []
diffFreqMatrixH x ((a,b):ys) = [(diffFreqMatrixH2 x a b 0 0)] ++ diffFreqMatrixH x ys
diffFreqMatrixH2 [] _ _ x y = x / y
diffFreqMatrixH2 (x:xs) a b summ num = if (((x!!a) /= NoRating) && ((x!!b) /= NoRating)) then diffFreqMatrixH2 xs a b (summ + ((x!!a) - (x!!b))) (num + 1) else diffFreqMatrixH2 xs a b summ num
supposedly it calculates an average I want but i'm getting this error
ERROR file:.\project.hs:98 - Cannot infer instance
*** Instance : Fractional (Rating a)
*** Expression : diffFreqMatrixH2
Helpers im using in case u want to take a look
matrixPairs :: Num a => a -> [(a,a)]
matrixPairs 0 = []
matrixPairs c = matrixPairsH 0 0 (c-1)
matrixPairsH a b c = [(a,b)] ++ if ((b == c) && (a == c)) then [] else if (b == c) then (if (a==c) then [] else matrixPairsH (a+1) 0 c ) else matrixPairsH a (b+1) c
differeneRatings :: Fractional a => Rating a -> Rating a -> a
differeneRatings NoRating (R a) = 0
differeneRatings (R a) NoRating = 0
differeneRatings NoRating NoRating = 0
differeneRatings (R a) (R b) = a - b
You use Rating as as if they were just numbers here:
(x!!a) - (x!!b)
You presumably need to use case or similar to pattern match on x!!a and x!!b to extract the number they contain. You can move the check for NoRating into that case match to simplify your code. For example:
case (x!!a, x!!b) of
(YesRating ra, YesRating rb) -> diffFreqMatrixH2 xs a b (summ + ra - rb) (num + 1)
_ -> diffFreqMatrixH2 xs a b summ num
Once you get this working the way you want, I encourage you to post to the code review StackExchange; your code can be cleaned up significantly to be both simpler and faster.
I'm new to Haskell and i don't understand why my guard won't accept it. here's my code. The guard should fire in case b is a divider of a.
gCF :: Integer -> Integer -> Integer;
gCF n p
| (p <= 0 || n <= 0) = error "Input should be positive"
| (p > n) = f p n
| otherwise = f n p
where
f :: Integer -> Integer -> Integer;
f a b
| (fromInteger (a `div` b) / 1 == a / b) = b
| otherwise = f a (b - 1)
Here's the error shown.
testscript.hs:168:28: error:
• No instance for (Fractional Integer) arising from a use of ‘/’
• In the first argument of ‘(==)’, namely
‘fromInteger (a `div` b) / 1’
In the expression: (fromInteger (a `div` b) / 1 == a / b)
In a stmt of a pattern guard for
an equation for ‘f’:
(fromInteger (a `div` b) / 1 == a / b)
|
168 | | (fromInteger (a `div` b) / 1 == a / b) = b | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
I think you make it the function more complex that necessary. Converting numbers between the Integer and Floating world can be dangerous, since it introduces rounding problems.
If I understand it correctly, you want to check if a is dividable by b. You can check this by verifying that mod a b == 0, and we are still in the integer worlds. So we can rewrite the program to:
gCF :: Integer -> Integer -> Integer
gCF n p | p <= 0 || n <= 0 = error "Input should be positive"
| p > n = f p n
| otherwise = f n p
where f a b | mod a b == 0 = b
| otherwise = f a (b-1)
Since a does not change in the recursive calls, we can factor that out:
gCF :: Integer -> Integer -> Integer
gCF n p | p <= 0 || n <= 0 = error "Input should be positive"
| otherwise = f (min p n)
where a = max p n
f b | mod a b == 0 = b
| otherwise = f (b-1)
We can also generalize the signature to let it work with any Integral type:
gCF :: Integral i => i -> i -> i
gCF n p | p <= 0 || n <= 0 = error "Input should be positive"
| otherwise = f (min p n)
where a = max p n
f b | mod a b == 0 = b
| otherwise = f (b-1)
i found a solution!
gCF :: Integer -> Integer -> Integer;
gCF n p
| (p <= 0 || n <= 0) = error "Input should be positive"
| (p > n) = floor (f (fromInteger p) (fromInteger n) (fromInteger n))
| otherwise = floor (f (fromInteger n) (fromInteger p) (fromInteger p))
where
f :: Float -> Float -> Float -> Float;
f a b c
| (fromInteger (floor (a / c)) == a / c) && (fromInteger (floor (b / c)) == b / c) = c
| otherwise = f a b (c - 1)
Here is my code:
xandy :: Element_w_Coord Cell -> Coord
xandy (e, (x, y)) = (x, y)
transition_world :: Ordered_Lists_2D Cell -> Element_w_Coord Cell -> Ordered_Lists_2D Cell
transition_world world (cell, (x, y)) = case (cell, (x, y)) of
(Head, (x, y))-> map_Ordered_Lists_2D Tail world
(Tail, (x, y)) -> map_Ordered_Lists_2D Conductor world
(Empty, (x, y)) -> map_Ordered_Lists_2D Empty world
(Conductor, (x, y)) -> map_Ordered_Lists_2D Head world
And here is the error message:
Sources/Transitions/For_Ordered_Lists_2D.hs:33:43:
Couldn't match expected type `Element_w_Coord e0 -> Cell'
with actual type `Cell'
In the first argument of `map_Ordered_Lists_2D', namely `Tail'
In the expression: map_Ordered_Lists_2D Tail world
In a case alternative:
(Head, (x, y)) -> map_Ordered_Lists_2D Tail world
Anyone likes to tell me what's wrong with my code pls?
Btw, here is the definition for
type Ordered_Lists_2D e = [Sparse_Line e]
data Sparse_Line e = Sparse_Line {y_pos :: Y_Coord, entries :: Placed_Elements e}
data Placed_Element e = Placed_Element {x_pos :: X_Coord, entry :: e}
type Placed_Elements e = [Placed_Element e]
map_Ordered_Lists_2D :: (Element_w_Coord e -> b) -> Ordered_Lists_2D e -> Ordered_Lists_2D b
map_Ordered_Lists_2D f world = case world of
l: ls -> map_line f l: map_Ordered_Lists_2D f ls
[] -> []
where
map_line :: (Element_w_Coord e -> b) -> Sparse_Line e -> Sparse_Line b
map_line f line = Sparse_Line {y_pos = (y_pos line), entries = map_elements f (y_pos line) (entries line)}
where
map_elements :: (Element_w_Coord e -> b) -> Y_Coord -> Placed_Elements e -> Placed_Elements b
map_elements f y elements = case elements of
c: cs -> Placed_Element {x_pos = (x_pos c), entry = f ((entry c), ((x_pos c), y))}: map_elements f y cs
[] -> []
Thanks for anyone who can good me some advice XD
The first argument of map_Ordered_Lists_2D is expected to be a function, but you are passing it Tail, which is of type Cell.
The following will type-check and should help you as a starting point:
transition_world world (cell, (x, y)) = case (cell, (x, y)) of
(Head, (x, y))-> map_Ordered_Lists_2D (const Tail) world
(Tail, (x, y)) -> map_Ordered_Lists_2D (const Conductor) world
(Empty, (x, y)) -> map_Ordered_Lists_2D (const Empty) world
(Conductor, (x, y)) -> map_Ordered_Lists_2D (const Head) world
(The function const takes a value and turns it into a function that always returns that same value, regardless of its argument.)
First of all try to make functions a bit smaller, like:
xandy :: Element_w_Coord Cell -> Coord
xandy (_, coord) = coord
transition_world :: Ordered_Lists_2D Cell -> Element_w_Coord Cell -> Ordered_Lists_2D Cell
transition_world world (cell, _) = map_Ordered_Lists_2D (transition_cell cell) world
where
transition_cell :: Cell -> Cell
transition_cell cell
| Head = Tail
| Tail = Conductor
| Empty = Empty
| Conductor = Head
and
map_Ordered_Lists_2D :: (Element_w_Coord e -> b) -> Ordered_Lists_2D e -> Ordered_Lists_2D b
map_Ordered_Lists_2D f world = map (map_line f) world
where
map_line :: (Element_w_Coord e -> b) -> Sparse_Line e -> Sparse_Line b
map_line f line = Sparse_Line {y_pos = y_pos line, entries = map_elements f (y_pos line) (entries line)}
map_elements :: (Element_w_Coord e -> b) -> Y_Coord -> Placed_Elements e -> Placed_Elements b
map_elements f y elements = let
place_e c = Placed_Element {x_pos = x_pos c, entry = f (entry c, (x_pos c, y))}
in
map place_e elements
I'm trying to write a prime number generator and utilizing MillerRabin formula check whether or not the number is prime before it returns the number back into me.
Here is my code below:
primegen :: Int -> IO Integer
primegen bits =
fix $ \again -> do
x <- fmap (.|. 1) $ randomRIO (2^(bits - 1), 2^bits - 1)
if primecheck x then return x else again
primesTo100 = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]
powerMod :: (Integral a, Integral b) => a -> a -> b -> a
powerMod m _ 0 = 1
powerMod m x n | n > 0 = join (flip f (n - 1)) x `rem` m where
f _ 0 y = y
f a d y = g a d where
g b i | even i = g (b*b `rem` m) (i `quot` 2)
| otherwise = f b (i-1) (b*y `rem` m)
witns :: (Num a, Ord a, Random a) => Int -> a -> IO [a]
witns x y = do
g <- newStdGen
let r = [9080191, 4759123141, 2152302898747, 3474749600383, 341550071728321]
fs = [[31,73],[2,7,61],[2,3,5,7,11],[2,3,5,7,11,13],[2,3,5,7,11,13,17]]
if y >= 341550071728321
then return $ take x $ randomRs (2,y-1) g
else return $ snd.head.dropWhile ((<= y).fst) $ zip r fs
primecheck :: Integer -> IO Bool
primecheck n | n `elem` primesTo100 = return True
| otherwise = do
let pn = pred n
e = uncurry (++) . second(take 1) . span even . iterate (`div` 2) $ pn
try = return . all (\a -> let c = map (powerMod n a) e in
pn `elem` c || last c == 1)
witns 100 n >>= try
I don't understand whats going on with the IO Bool. And I'm getting the following error...
Couldn't match expected type `Bool' with actual type `IO Bool'
In the return type of a call of `primecheck'
In the expression: primecheck x
In a stmt of a 'do' block: if primecheck x then return x else again
If I change the IO Bool to just a normal Bool, they will give me this:
Couldn't match expected type `Bool' with actual type `m0 a0'
Thanks for the help guys! I appreciate it.
if primecheck x then return x else again
is not valid because primecheck x returns a value of type IO Bool. You want to sequence the monad with do notation or something like:
primecheck x >>= (\val -> if val then return x else again)
Since primecheck returns IO Bool, when you call it in primegen, you need to sequence it rather than calling it like a pure function.
primegen :: Int -> IO Integer
primegen bits =
fix $ \again -> do
x <- fmap (.|. 1) $ randomRIO (2^(bits - 1), 2^bits - 1)
success <- primecheck x
if success then return x else again