Given a sequence such as x = [5, 9, 0, -14], I need to find the min and max without sorting the sequence or by using min() and max(). I've been instructed to use loop constructs, but I can't figure out how to do so. Help is greatly appreciated.
>>1
Quit your programming class, you're never going to competent. Put your energy into something you have a chance at succeeding at.
Name:
Anonymous2007-02-09 16:53
minmax :: [a] -> (a, a)
minmax x = (head . sort $ x, last . sort $ x)
Name:
Anonymous2007-02-09 16:55
>>5
without sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting without sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting the sequencewithout sorting the sequencethe sequence
it's good that no one knows which anonymous I am. I wrote >>12 without reading the end of >>10, and thought >>11 is replying to the JavaScript thread
Name:
Anonymous2007-02-10 6:25
>>10
It's not a hypothesis, it's a block of code. Since it's a block of code, it can't possibly lack logical argumentation (any more than a block of wood can lack logical argumentation), hence it's not an ipsedixitism either.
This is why Python sucks x = [5, 9, 0, -14]
max = x[0]
min = x[0]
for num in x:
if num < min:
min = num
if num > max:
max = num
What was the algorithm again? You fail :D
Name:
Anonymous2007-02-10 9:38
>>17
the same happens if you on purpose remove braces from a language that uses them as block delimiters. you seem to assume that it's common for indentation to get trimmed off, but that argument obviously does not hold water, just because all the modern ides and editors would never do that by accident. logic failget.
>>14
if I was referring to the code, I would have said so. the unfamiliar term must have confused you. I must remember to dumb it down for nincompoops like you next time
Name:
Anonymous2007-02-10 9:42
One word, a forced cultural item. Meme over.
Name:
Anonymous2007-02-10 10:10
OK GUYS HERES A REAL HASKELL SOLUTION NOTE I DID NOT ACHIEVE SATORI YET OK
minmax :: [a] -> (a, a)
minmax l = (foldr (min) (head l) l, foldr (max) (head l) l)
One words, no indentation. Thread over.
Name:
Anonymous2007-02-10 11:07
Here's a REAL Haskell solution:
import Control.Arrow ((&&&))
import Data.List (foldl1')
minmax :: Ord a => [a] -> (a,a)
minmax = foldl1' min &&& foldl1' max
Name:
Anonymous2007-02-10 13:29
>>23
I'm still trying to grok monads and now arrows are the cool thing :(
Name:
Anonymous2007-02-10 17:40
>>24
Just give it up, something new will come along and double the "syntax," making it impossible for even Post-Enlightenment Buddha to read Haskell.
Name:
Anonymous2007-02-11 8:19
>>22 >>23
Both of these do two loops over the input data, assuming the compiler can't fuse the two. Here's a single-pass version:
minmax lst = foldl (\(a, b) c -> (min a c, max b c)) (head lst, head lst) lst
Wankers can proceed to optimize it further with a strict foldl. As if the compiler didn't catch that when compiling with -O.
Name:
Anonymous2007-02-11 9:20
>>26
Variable with more than one letter? BIG WARNING!!
Name:
Anonymous2007-02-11 9:21
>>26
Also, it is Haskellic to put a space after the lambda symbol.
Name:
Anonymous2007-02-11 12:17
Personally I hate using lst as a variable name because it looks too much like 1st.
Name:
Anonymous2007-02-11 14:53
>>28
No, it's just coddling to weak editors that mistake a \( for a quoted open brace and fail to match closing braces appropriately.
Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy