Questions tagged [pattern-guards]

In function definitions by clauses with pattern matching, pattern guards allow for additional pattern matching inside guards, to determine which clause is chosen for execution. For Boolean guards use [guard-clause] tag.

In function definitions by clauses with pattern matching, pattern guards allow for additional pattern matching inside guards, to determine which clause is chosen for execution. For Boolean guards see .

References

27 questions
33
votes
1 answer

Haskell - guard inside case statement

I am going through Learn you a haskell book, and in Chapter 8 there is a snippet of code which looks like this data LockerState = Taken | Free deriving (Eq, Show) type Code = String type LockerMap = Map.Map Int (LockerState, Code) lookup' :: Int ->…
skgbanga
  • 2,477
  • 2
  • 20
  • 29
32
votes
2 answers

What does a comma in the guard syntax do?

In a code base I'm reading, I found a function declaration like this (some parts are missing): filepathNormalise :: BS.ByteString -> BS.ByteString filepathNormalise xs | isWindows, Just (a,xs) <- BS.uncons xs, sep a, Just (b,_) <- BS.uncons xs,…
typetetris
  • 4,586
  • 16
  • 31
21
votes
1 answer

Warning that pattern guard is non-exhaustive even though it is

I'm observing an interesting behavior when using pattern matching with pattern guards and all warnings turned on {-# OPTIONS_GHC -Wall #-} module Mood where data Mood = Happy | Indifferent | Sad deriving Show flipMood…
raichoo
  • 2,557
  • 21
  • 28
14
votes
2 answers

What does left arrow <- mean outside a do block?

I came across with the following code recently and it bothers me a lot lowerSafeForeignCall dflags block | (entry, middle, CmmForeignCall { .. }) <- blockSplit block = do -- do block stuffs -- Block doesn't end in a safe foreign call: | otherwise…
Theodora
  • 571
  • 4
  • 11
13
votes
3 answers

Is there, in Haskell, something similar to sub-guards?

I'm writing a program on the classification of musical intervals. The conceptual structure is quite complicated and I would represent it as clearly as possible. The first few lines of code are a small extract that works properly. The second are the…
Alberto Capitani
  • 1,039
  • 13
  • 30
11
votes
2 answers

Reusing patterns in pattern guards or case expressions

My Haskell project includes an expression evaluator, which for the purposes of this question can be simplified to: data Expression a where I :: Int -> Expression Int B :: Bool -> Expression Bool Add :: Expression Int -> Expression Int …
guhou
  • 1,732
  • 12
  • 32
5
votes
1 answer

Not sure why this pattern guard matches

Learning Haskell and I am not sure why I don't get the expected result, given these definitions: instance Ring Integer where addId = 0 addInv = negate mulId = 1 add = (+) mul = (*) class Ring a where addId :: a --…
j-a
  • 1,780
  • 1
  • 21
  • 19
5
votes
2 answers

"with" guard in pattern matching

I read about pattern guards in ocaml-patterns which shows this type of guard: match x with | pat with g = y -> z | ... | pat with g = y -> z In OCaml 4.02 however, this does not seem to work (Syntax error: pattern expected.). So the question is: is…
4
votes
2 answers

How do you match with guards in Racket?

In Scala you can do something like this: def times[A](item: A, number: Int): List[A] = number match { case n if n <= 0 => Nil // Nil = '() case _ => // equivalent to [_ (cons item (times item (- number 1)))] item :: times(item, number -…
Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
4
votes
2 answers

Erlang multiple Guards on `when`

Fellow stackoverflow-ers, I am currently learning Erlang. Could someone point me why do I get an illegal guard expression with this guard? add_new_prime(Idx, Primes, Ref) when length(Primes) =:= 0 ; math:sqrt(Idx) < hd(Primes) -> Ref ++ [Idx]; If I…
marctrem
  • 800
  • 8
  • 15
4
votes
2 answers

less.css if variable is true guard

I wonder if there is a better solution (or if my solution is even right), to create if statement like behavior with variables and guards. Goal: If variable is set to true, compile the code (works) If variable is set to anything else, ignore the…
InitArt
  • 97
  • 1
  • 1
  • 7
3
votes
1 answer

My code results in parse error because of my use of guards

I have the following code: parseExpr :: [String] -> (Ast,[String]) parseExpr [] = error "Incorrect" parseExpr (s:ss) | all isDigit s = (Tall (read s),ss) | s == "-" = let (e,ss') = parseExpr ss in (Min e,ss') | s == "+" =…
3
votes
3 answers

How do I let a function in Haskell depend on the type of its argument?

I tried to write a variation on show that treats strings differently from other instances of Show, by not including the " and returning the string directly. But I don't know how to do it. Pattern matching? Guards? I couldn't find anything about that…
Turion
  • 5,684
  • 4
  • 26
  • 42
2
votes
2 answers

Scala matching List of tuples with guards

I new to Scala. As an exercise I am trying to write a match statement over a list of tuples with guards. I am aware that a map would solve the problem but I am trying to gain understanding into pattern matching. I seek to write a function that takes…
Stereo
  • 1,148
  • 13
  • 36
2
votes
1 answer

Haskell function checking if number is odd, without using the odd function

Can anyone help me with that? I am trying to write a function checking if an x is odd, without using the odd function. Like this it does not work but i don't know why. ugerade :: Integral a => a -> Bool ugerade x |x elem oddList = True …
Mauritius
  • 265
  • 1
  • 8
  • 23
1
2