How to I properly use the min function in Haskell code? [closed] - haskell

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
main = print $ smallestFactor 15
factors n = [x | x <- [2..n], n `mod` x == 0]
smallestFactor n = min[factors n]
I understand that it's a pretty trivial question but someone told me that I could use min get grab out the smallest value of a element list. Though, I'm not exactly sure how one is suppose to use it.

You mean minimum :: Ord a => [a] -> a. Plus, you're erroneously using square brackets when you should be using parentheses. Don't use square brackets when you're not explicity defining a list!
Here's the corrected part:
smallestFactor n = minimum (factors n)
The rest of your code is fine.

Related

outputting a result is much slower that just executing its function? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
we are doing the project euler challenges with Haskell. Currently we're at no 7 "find the 10001st prime number"
Now we have a simple filter algorithm, nothing fancy but the calculation time varies widely with how you call the function.
primes = filterPrime [2..]
where filterPrime (p:xs) =
p : filterPrime [x | x <- xs, x `mod` p /= 0]
when using ghci with primes and just stopping when you see the numbers go over 100000 or so it usually runs ~3 seconds or so
when using primes !! 10001 we have significantly longer runtimes up to 20 or 25 seconds. Once the list is filled, you can call the 10001st element instantly both ways.
Does anyone have a good explanation on how this works or why it takes so long in the second method?
local problem, needs own troubleshooting

I migrated from python 2.7 to 3.8.5, now my code won't run. I am showing UnboundLocalError: local variable 'Url_name' referenced before assignment [duplicate]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Python:
I have researched a lot into this, but as a beginner I don't understand the solutions, let alone apply them to my simple problem:
def min_max(xs):
xs = []
for i in xs:
y = (min, max)
return y
"local variable 'y' referenced before assignment"
I don't understand why this does NOT work!!
I've defined Y in the line before return, i know there is another way to do it, but i really need to know why this method is wrong!
Thanks!
assuming that min and max are defined somewhere else....
The loop for i in xs will go around once for each item in xs. Since xs is empty, the loop will not run at all. So, y is never set.

Lexical fault when using ''BOOM!'' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I type this into my texteditor:
boombang xs = [ if x < 10 then ”BOOM!” else ”BANG!”
But when trying to load it, my terminal denies it and and says:
Probably some dumb rookie mistake, but I can't seem to find the solution...:/
There are two problems here.
The first is, you're not using plain ASCII quotation marks (U+0022) ". You're using right quotation marks (U+201D) ”. There's probably a keyboard setting you're using that's causing this, and using certain text editing programs can also prevent this.
Secondly, you seem to be either missing a right bracket. As pointed out, this is probably meant to be part of a list comprehension. You need to write all of the list comprehension for it to work.
The corrected code is:
boombang xs = [if x < 10 then "BOOM!" else "BANG!" | x <- xs]

what does forward slash mean in haskell? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am learning Haskell, studying for a midterm. there is a test quiz I'm looking at at the moment with questions on type inference. some of the questions contain forward slashes and I have no idea what they represent. the goal is to determine the type of the function by looking at its variables. here are some examples of the questions im supposed to find the type of:
f1 x = (/x)
f3 x = (x/)
f5 = (1/)
f6 = (/2)
I have googled with no luck, what does the forward slash mean?
These are sections: the expression (/x) is equivalent to \y -> y / x and (x/) is equivalent to \y -> x / y.

Using do notation with if/else [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to use do notation to only return a list element if it's even:
ghci> do { x <- [1,2,3]; if (even x) [x] else []}
<interactive>:43:36: parse error on input `else'
What am I doing wrong?
In addition, please note if this code is non-idiomatic.
You forgot the "then".
Also, i'm not really sure which Monad is this..
do { x <- [1,2,3]; if (even x) then [x] else []}

Resources