How to write a case Maybe statement using where - haskell

In Haskell, I am trying to write something like
function x = do
y <- get x
put y
let z = Map.lookup y
case y of
Nothing -> Nothing
Just y -> do... (something)
I want to replace the "let..case of" block with something like a "where..case of" block or "where" block if possible. How can I do so?

You can define a function with pattern matching in a where block. (I don't know, if it is a better solution because you didn't explain very well what does the function do, but it is possible):
function x = do
y <- get x
put y
let z = Map.lookup y
fn y
where fn Nothing = Nothing
fn (Just n) = do... (something)
Also, note that you can use fmap:
function x = do
y <- get x
put y
let z = Map.lookup y
fmap fn y
where fn n = do... (something)

Related

Block scope in Haskell?

I've found block scope {and } in JavaSciprt after ES6 is useful in development, and frequently use for testing small code blocks including the same variable names in different versions side by side.
How do you do in Haskell? What's the best prctice?
If you have some code like this:
foo x y = do
print x
print y
And you want to temporarily test changing the value of x then you can just write:
foo x y = let x = 2 in do
print x
print y
This let syntax creates a new scope and "overwrites" (shadows) any variables with the same name that were previously bound. You can use this let almost everywhere, e.g.:
foo x y = do
let x = 2 in print x
print y
Or even:
foo x y = do
print (let x = 2 in x)
print y
It is really flexible.
There's a few choices, depending on what context you're looking for. I think the thing most likely to be useful to you is that each binding can be associated with a where block, and the variables in each block are independent. So, for example:
foo = component1 * component2 where
component1 = x + y where
x = 32
y = sqrt (magnitude velocity)
component2 = x + y where
x = magnitude acceleration
y = 5.5
In each case, the x and y are in scope only for the component that's currently being defined.
One place that doesn't always work well is in do blocks. There's two reasons for this: first, the associated where block's scope doesn't include any of the variables bound by statements in the do block, and second, because sometimes the thing you want is differing scopes for differing parts of the do block. For those cases, let/in works better. For example:
foo = do
let x = print "hi"
y = putStrLn "hi"
in do
x
y
-- x and y are no longer in scope here
v <- readLn :: IO Int
let x = print [v] -- v is in scope, unlike a where block
y = print v
in do
x
y
Again, in each case, the x and y are in scope for the body of the nested do block, but not in scope in the outer do block. It is also possible to add something to the outer do block's scope, though of course the syntax has to be different (or how would the compiler know which one you wanted?). The difference is that you leave off the in, so:
foo = do
v <- readLn :: IO Int
let w = v + v
print w

whats the advantage of using guards in Haskell?

count_instances :: (Int)->([Int])->Int
count_instances x [] = 0
count_instances x (t:ts)
| x==t = 1+(count_instances x ts)
| otherwise = count_instances x ts
i just want to know whats so good about using guards in this Question ?
A guard can be a way to write only one half of an if-then-else expression; you can omit the else and have a partial function.
-- Leave the function undefined for x /= y
foo x y | x == y = ...
You can do the same with a case statement, but it's more verbose
foo x y = case x == y of
True -> ...
It's also easier to list several unrelated conditions as a set of alternatives than it is with a nested if-then-else or case expressions.
foo x y | p1 x y = ...
foo x y | p2 x y = ...
foo x y | p3 x y = ...
foo x y = ...
vs
foo x y = if p1 x y then ...
else (if p2 x y then ...
else (if p3 x y then ... else ...))
Patterns with guards are probably the most concise way to write code that otherwise would require nested case/if expressions.
Not the least advantage is that a where clause applies to all the guards right hand sides. This is why your example could be even more concise:
count_instances :: (Int)->([Int])->Int
count_instances x [] = 0
count_instances x (t:ts)
| x==t = 1+rest
| otherwise = rest
where rest = count_instances x ts
A guard is haskell's most general conditional statement, like if/then/else in other languages.
Your code shows a straight forward implementation of counting contents of a list equal to a given parameter. This is a good example to learn how haskell's recursion works.
An alternative implementation would be
count_instances :: Int -> [Int] -> Int
count_instances i = length . filter (==i)
that reuses already existing functions from the Prelude module. This is shorter and probably more readable.

Why you can't use substitution inside a do block while you can use let (binding)? [duplicate]

This question already has answers here:
Equal (=) Vs left arrow (<-) symbols in haskell
(4 answers)
Closed 6 years ago.
Why can't you use substitution inside a do block?
This code works fine.
test :: (x -> x) -> [x] -> [x]
test f a = map f a
main :: IO ()
main = do
let sq x = x * x :: Int
let ret = test sq [1,2,3]
print ret
But if you remove the let's inside the do block, I got compile errors.
parse error on input ‘=’
Perhaps you need a 'let' in a 'do' block?
e.g. 'let x = 5' instead of 'x = 5'
Is "let x = y" equivalent to "x <- y" inside a do block?
So if the right hand side returns a IO something, you need to use let's (or <-) ?
I know it's a dummy question but I always get compile error with this.
eii
let is how you assign a value to a name inside a do block. let x = y is not equivalent to x <- y. Inside a do block, let desugars like this.
do let x = y
...
becomes
let x = y
in do ...
whereas
do x <- y
...
becomes
do y >>= (\x -> ...)
Bare = is only for top-level assignments, for defining functions and constants (which are a lot like 0-argument functions).

Can we use 'where' in a lambda function in Haskell?

I was wondering if we can use where in an anonymous function or not. I tried to do it in this way:
\x -> k where k = x+1
But this gives a parse error on 'where'.
You can use where in certain expressions within a lambda expression, but not just inside.
f = \x ->
case x of
Nothing -> 12
Just y -> z * 2
where z = y + 7

Is there a lazy functional (immutable) language where functions have intermediate variables+return?

I apologize if this has an obvious answer. I would like to find a lazy functional programming language where the following pseudo code makes sense:
let f = function(x) {
let y = x*x // The variables y and z
let z = y*2 // are local
return z
}
This is, of course, doable in a functional, declarative language like haskell. Either with let bindings or where keyword. These are just chained together in one expression.
--local variables with 'where'
f :: Int -> Int
f x = z where
z = y*2
y = x*x
--local variables with let
g :: Int -> Int
g x =
let y = x*x
z = y*2
in z
https://wiki.haskell.org/Let_vs._Where

Resources