You are not logged in.

#1 2011-01-23 21:30:08

chris-kun
Member
From: SF Bay Area
Registered: 2010-09-07
Posts: 235
Website

Starting off in haskell

So I've been learning haskell this past week, and I thought it might be a good idea to get some feedback from guys who know what they're doin' tongue. I just finished euler #4 here, so if anyone can see anything that could be implemented in a better way, stylistic changes, etc. or maybe my code is just completely unreadable and I should work on that first xD.

But yeah, much appreciated smile


[home page] -- [code / configs]

"Once you go Arch, you must remain there for life or else Allan will track you down and break you." -- Bregol

Offline

#2 2011-01-23 21:47:53

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: Starting off in haskell

I didn't try your code and it might be more efficient, but I solved it with a lot less code:

main = 
  print $ maximum $ filter isPalindrome' [ x*y | x<-[1..999], y<-[1..999]]

isPalindrome' :: (Integral a) => a -> Bool
isPalindrome' = isPalindrome . digitsRev

isPalindrome :: (Eq a) => [a] -> Bool
isPalindrome x = x == reverse x

digitsRev :: (Integral a) => a -> [a]
digitsRev 0 = []
digitsRev n = remainder : digitsRev quotient
  where (quotient, remainder) = quotRem n 10

This was a while ago though, and I made it pretty quick and didn't bother optimizing as it runs in less than a second smile

Offline

#3 2011-01-23 22:00:47

chris-kun
Member
From: SF Bay Area
Registered: 2010-09-07
Posts: 235
Website

Re: Starting off in haskell

I tried a similar solution to yours (although it was in lua) where I just ran through every possibility and just took the largest palindrome, but it took at least a minute to run, so I had to figure out a way to list all the products in order, and return the first one found.

actually your code above runs for a minute and produces a stack overflow for me. maybe my machine just sucks balls, lol.

Last edited by chris-kun (2011-01-23 22:04:44)


[home page] -- [code / configs]

"Once you go Arch, you must remain there for life or else Allan will track you down and break you." -- Bregol

Offline

#4 2011-01-23 22:04:17

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: Starting off in haskell

chris-kun wrote:

I tried a similar solution to yours (although it was in lua) where I just ran through every possibility and just took the largest palindrome, but it took at least a minute to run, so I had to figure out a way to list all the products in order, and return the first one found.

actually your code above runs for a minute and produces a stack overflow for me. maybe my machine just sucks balls, lol.

Really? Did you compile it with -O2 ? It runs in about 0.40 seconds on my core 2 duo at 1.8GHz, not such an exceptionally powerful machine I'd think...

Offline

#5 2011-01-23 22:09:16

chris-kun
Member
From: SF Bay Area
Registered: 2010-09-07
Posts: 235
Website

Re: Starting off in haskell

ah you're right compiled it runs in 2.3 secs for me, although mine still runs in 0.043 tongue


[home page] -- [code / configs]

"Once you go Arch, you must remain there for life or else Allan will track you down and break you." -- Bregol

Offline

#6 2011-01-23 23:09:03

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: Starting off in haskell

Yeah your solution is probably way more efficient smile
I just tried the brute force method first and it was fast enough smile

Offline

#7 2011-01-29 20:56:55

dagle
Member
Registered: 2009-01-04
Posts: 13

Re: Starting off in haskell

nvm, speedbump in my brain...

Last edited by dagle (2011-01-29 21:05:46)

Offline

#8 2011-02-16 19:49:04

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Starting off in haskell

Offline

#9 2011-02-16 19:52:37

chris-kun
Member
From: SF Bay Area
Registered: 2010-09-07
Posts: 235
Website

Re: Starting off in haskell

yeah I get that you can bruteforce it.. I was trying to add in the functionality of ordering all of the products from largest to smallest, though tongue


[home page] -- [code / configs]

"Once you go Arch, you must remain there for life or else Allan will track you down and break you." -- Bregol

Offline

Board footer

Powered by FluxBB