Questions tagged [guard-clause]

In function definitions by clauses with pattern matching, guards are Boolean expressions which augment pattern matching with the possibility to skip a pattern even if an argument structure matches the pattern.

In function definitions by clauses with pattern matching, guards are Boolean expressions which augment pattern matching with the possibility to skip a pattern even if an argument structure matches the pattern.

See also:

References

64 questions
35
votes
5 answers

Refactoring Guard Clauses

What approaches do people take (if any) in managing guard clause explosion in your classes? For example: public void SomeMethod(string var1, IEnumerable items, int count) { if (string.IsNullOrEmpty(var1)) { throw new…
Kane
  • 16,471
  • 11
  • 61
  • 86
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
26
votes
18 answers

How should I rewrite a very large compound if statement in C#?

In my C# code, I have an if statement that started innocently enough: if((something == -1) && (somethingelse == -1) && (etc == -1)) { // ... } It's growing. I think there must be 20 clauses in it now. How should I be handling this?
Michael Broschat
  • 875
  • 3
  • 10
  • 17
23
votes
4 answers

Pattern matching identical values

I just wondered whether it's possible to match against the same values for multiple times with the pattern matching facilities of functional programming languages (Haskell/F#/Caml). Just think of the following example: plus a a = 2 * a plus a b = a…
Dario
  • 48,658
  • 8
  • 97
  • 130
21
votes
3 answers

is there any way for multiple where statement in Haskell

i tried to write 3-4 where statement in a one function but i get error and couldnt do it , i tried to do something like that : foo x= | x == foo1 = 5 | x == foo2 =3 | x == foo3 =1 | otherwise =2 where foo1= samplefunct1 x foo2= samplefunct2…
16
votes
3 answers

Auto-implemented properties with non null guard clause?

I do agree with Mark Seeman's notion that Automatic Properties are somewhat evil as they break encapsulation. However I do like the concise syntax, readability and convenience they bring. I quote: public string Name { get; set; } The problem with…
Can Gencer
  • 8,822
  • 5
  • 33
  • 52
15
votes
1 answer

F# Incomplete pattern matches on this expression when using "when"..Why?

I have this simple F# function: let compareNum x = let y = 10 match x with | _ when x = y -> 0 | _ when x > y -> 1 | _ when x < y -> -1 However, F# compiler gives me "Incomplete pattern matches on this expression" warning. In…
kimsk
  • 2,221
  • 22
  • 23
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
1 answer

Is it possible to use guards in function definition in idris?

In haskell, one could write : containsTen::Num a => Eq a => [a] -> Bool containsTen (x : y : xs) | x + y == 10 = True | otherwise = False Is it possible to write something equivalent in Idris, without doing it with ifThenElse (my real case…
Molochdaa
  • 2,158
  • 1
  • 17
  • 23
11
votes
1 answer

Guard checking of lambdas

I usually perform guard checking like so: public void doStuff(Foo bar, Expression> pred) { if (bar == null) throw new ArgumentNullException(); if (pred == null) throw new ArgumentNullException(); // etc... } I've seen this…
h bob
  • 3,610
  • 3
  • 35
  • 51
10
votes
4 answers

Pattern matching with guards vs if/else construct in F#

In ML-family languages, people tend to prefer pattern matching to if/else construct. In F#, using guards within pattern matching could easily replace if/else in many cases. For example, a simple delete1 function could be rewritten without using…
pad
  • 41,040
  • 7
  • 92
  • 166
10
votes
4 answers

Match with empty sequence

I'm learning F# and I've started to play around with both sequences and match expressions. I'm writing a web scraper that's looking through HTML similar to the following and taking the last URL in a parent with the paging…
JoshVarty
  • 9,066
  • 4
  • 52
  • 80
9
votes
3 answers

Guard clauses in prolog?

Do they exist? How are they implemented? The coroutining predicates of SWI-Prolog (freeze, when, dif etc.) have the functionality of guards. How do they fit in the preferred Prolog programming style? I am very new to logic programming (with Prolog…
user1812457
7
votes
4 answers

Guard inside 'do' block - haskell

I want to write a simple game "guess number" - with n attempts. I want to add some conditions and hits. Is it possible to use guards inside do block ? Here is my code: game = return() game n = do putStrLn "guess number: 0-99" …
tomasz pawlak
  • 151
  • 1
  • 7
7
votes
4 answers

Does pattern match in Raku have guard clause?

In scala, pattern match has guard pattern: val ch = 23 val sign = ch match { case _: Int if 10 < ch => 65 case '+' => 1 case '-' => -1 case _ => 0 } Is the Raku version like this? my $ch = 23; given $ch { when Int…
chenyf
  • 5,048
  • 1
  • 12
  • 35
1
2 3 4 5