Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Haskell

Name: Anonymous 2009-01-27 20:20

Please help with my Haskell code. It is supposed to return the maximum element in a list but it doesn't work

lol :: [a] -> Nothing | a
lol [] = Nothing
lol [x] = x
lol (x:xs) = if x > lol xs then x else lol xs

Name: Anonymous 2009-01-27 20:29

listMax []     = Nothing
listMax (x:xs) = Just $ foldr (\x y -> if x > y then x else y) x xs

Name: Anonymous 2009-01-27 20:55

lol :: [a] -> Nothing | a
Should be [a] -> Maybe a. I'm curious as to where you got the idea that this was possible.

lol (x:xs) = if x > lol xs then x else lol xs
Two problems here. if x > lol xs means that lol xs has to be evaluated before any actual work can be done. Thus it may keep the whole list in memory, since it needs lol xs afterwards as well. The compiler may eliminate the second call to lol xs in which case it's not as bad, but it still wastes stack space because of the second problem, which is that it's not tail recursive.

A similar in spirit, but fixed, version (it would be better to use a fold, however see >>2):
listMax :: [a] -> Maybe a
listMax [] = Nothing
listMax (x:xs) = Just $ iter x xs where
  iter m [] = m
  iter m (x:xs) = iter (if x > m then x else m) xs

Name: Anonymous 2009-01-27 20:58

>>2
Thanks man
What is Just here

Name: Anonymous 2009-01-27 21:00

Name: Anonymous 2009-01-27 21:14

>>3
I have no idea. I obviously know nothing about Haskell's typing system, so I was trying to learn by example. The only reason I didn't try Maybe was because when I looked it up it talked about monads ands Just, neither of which I understand.

lol (x:xs) = if x > lol xs then x else lol xs
Thanks for the explanation. I kind of expected the runtime would be pig disgusting here. It was more of an attempt to get the function to compile than anything.

Name: Anonymous 2009-01-27 21:41

>>6
It's probably a good idea to read http://learnyouahaskell.com/ to get to grips with this sort of stuff. Maybe is introduced at http://learnyouahaskell.com/making-our-own-types-and-typeclasses#type-parameters (but it's probably best to read it more-or-less in order, at least for the first few chapters).

http://www.haskell.org/haskellwiki/Performance/Accumulating_parameter is also relevant.

Name: Anonymous 2009-01-27 21:59

I'd let a fine japanese girl recurse my tail, if you catch my drift.

Name: Anonymous 2009-01-27 22:07

Haskell sucks. Stop learning it. Stop using all OOP and do real work in a man's language.

Name: Anonymous 2009-01-27 22:20

Haskell
OOP
wat

Name: Anonymous 2009-01-27 22:51

>>1-7
what.
lol :: (Ord a) => [a] -> Maybe a
lol [] = Nothing
lol x = Just $ maximum x

Name: Anonymous 2009-01-27 22:52

>>11
Way to miss the point of the exercise.

Name: Anonymous 2009-01-27 22:54

>>12
Way to miss the point of the language.
Laziness is what Haskell is all about.

Name: Anonymous 2009-01-28 9:11

>>11
Good point about the Ord a => though.

Name: Anonymous 2009-01-28 11:17

What is Ord a =>

Name: Anonymous 2009-01-28 12:02

>>15
A type constraint. It means that a has to be an instance of Ord, that is, it must be comparable.

Name: Anonymous 2009-01-28 12:26

>>13
wrong, haskell is about writing interpreters for toy languages EASILY and then writing factorial in them then wanking for hours on end

Name: Anonymous 2009-01-28 12:49

>>2
*Main> listMax [1..1000000]
Just *** Exception: stack overflow

>>3
*Main> listMax [1..1000000]
Just *** Exception: stack overflow
(though it took like 5 times longer than previous "solution", ololo)

All you people suck at haskell.

Also, this proves that haskell itself sucks: there is no no other language formally supporting tail recursion where any obvious solution would stackoverflow.

Name: Anonymous 2009-01-28 12:51

Recursion is bad design. Rewrite without recursion.

Name: Anonymous 2009-01-28 13:18

>>19
FrozenVoid?

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !FrOzEn2BUo 2009-01-28 13:25

Obviously. Recursion is wrong and abstracts away the  loops  
which when implemented rationally are much faster and waste far less resources.

_________________________
orbis terrarum delenda est
 http://xs135.xs.to/xs135/09042/av922.jpg

Name: Anonymous 2009-01-28 13:29

>>21
Clearly hasn't read his SICP today.

Name: Anonymous 2009-01-28 13:34

>>22
Clearly has been brainwashed by SICP.

Name: Anonymous 2009-01-28 13:42

>>23
If he had read it, he would know that recursion can be used to write more elegant solutions and that iterative processes can be implemented in a recursive manner. A blanket statement like ``recursion is wrong" merely shows his ignorance.

Also, unless he is programming on systems with limited resources (e.g. I program microcontrollers), why should he care about some resources being wasted. Moore's law is continuing to hold and should for some time.

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !FrOzEn2BUo 2009-01-28 13:44

>>24
A ℬℒÅℕҚℇŦ post like >>18 shows  why its wrong

_________________________
orbis terrarum delenda est
 http://xs135.xs.to/xs135/09042/av922.jpg

Name: Anonymous 2009-01-28 14:16

>>25
Programming is not science. One example does not prove that something is wrong. It just shows that in this case recursion was not the most efficient solution.

Name: Anonymous 2009-01-28 14:27

>>26
Countless programs which will be stackoverflow'ed and run out of memory will make you think otherwise,
but for now you can read your newbie books and write it your way. Someday you might realize why your abstractions fail.

Name: Anonymous 2009-01-28 14:35

>>27
And, as we'll see later on in the lesson, programming isn't really about computers, either. So what is programming really about?

Name: Anonymous 2009-01-28 15:20

>>27
(hope I'm not answering to that coldsomething homo) the problem is not with recursion (that can be and sometimes is as fast as loops, you'd better read your SICP nao, until the Snake gets you), the problem is specifically with Haskell and its lazy evaluation by default policy.

I've thought about it for a while and I'm more or less sure that it's plain impossible to sum a million ints in haskell without using these heretical foldl' or seq. Prove me wrong please, okay, thanks.

Name: Anonymous 2009-01-28 16:20

>>18
To get tail call optimization you have to compile with -O.

Name: Anonymous 2009-01-28 16:24

>>29
You could quite easy hand-write sum. It would be more or less equivalent to using foldl' though. Also, there's nothing wrong with foldl' and seq (and $! and bang patterns).

It's not such a huge problem though, especially considering that ghc -O runs >>2 and >>3 quite happily. Excessive laziness has rarely caused performance problems in my code, and it's usually pretty easy to fix when it does.

Name: Anonymous 2009-01-28 16:40

I've found that performance problems in Haskell were (more often than not) caused by not understanding the implications of laziness and how it affects the cost of certain algorithms. I've seen numerous cases when a strict algorithm  was faster/used less memory than my initial naive implementation. In most cases, I managed to analyse the algorithm and refactor into a lazy algorithm that performs just as well.

I don't really blame people for not understanding laziness as well as they should, it is truly a tool that is one level above the rest.

Name: Anonymous 2009-01-28 18:14

>>32
I've found that performance problems in Haskell were (more often than not) caused by not understanding the implications of laziness
So it's like most other languages, where performance problems are caused by similar misunderstandings of language feature? (eg. not understanding garbage collection/free/delete?

Name: Anonymous 2009-01-28 18:17

>>33
It's probably more akin to not understanding reference semantics or scoping issues.

Name: Anonymous 2009-01-28 18:31

>>34
Actually it's more akin to me haxing your anus.

Name: Anonymous 2009-01-29 9:01

>>34
Yes. The situation is that the programmer doesn't understand enough about the tools to exploit them to their potential.

Name: ​​​​​​​​​​ 2010-10-22 12:26

Name: Anonymous 2010-12-06 9:15

Back to /b/, ``GNAA Faggot''

Name: Anonymous 2011-02-03 1:24

<

Don't change these.
Name: Email:
Entire Thread Thread List