LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-29-2012, 02:00 PM   #1
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
[HASKELL] Style advice/guidelines for code snippet


I have the following haskell code, but I don't feel that it's really in keeping with haskell's style:

Code:
import Data.List (nub)

main :: IO ()
main = do
        input <- getLine
        let number = read input :: Integer
        let factors = factorise number 2
        let unique_factors = nub factors
        let answer = list_product unique_factors
        print answer

list_product :: [Integer] -> Integer
list_product [x] = x
list_product (x:s) = x * list_product s

factorise :: Integer -> Integer -> [Integer]
factorise n prime
        | prime > n = []
        | (mod n prime == 0) = prime : factorise (div n prime) prime
        | otherwise = factorise n (prime+1)
Specifically, I'm worried by the high number of 'let' statements I have - but any more general comments on the code would be appreciated as well.

The code prints out the product of the unique factors of a number.

Thanks,
 
Old 06-02-2012, 11:51 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
but I don't feel that it's really in keeping with haskell's style:
I'm only vaguely familiar with Haskell, so I can't really say where it's "Haskell's style" or not, but I do have a few comments.
Quote:
Specifically, I'm worried by the high number of 'let' statements I have
I expect you could at least combine them into a single use of the keyword let with multiple variables.

In factorise, you only need to check up to the square root of n.

list_product could written as a fold (or reduction) over the list.
 
1 members found this post helpful.
Old 06-05-2012, 07:34 AM   #3
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632

Original Poster
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Thank you very much my brain's far too used to imperative programming. I might give up and learn LISP before I approach Haskell...

I'll mark it as SOLVED, as there doesn't seem to be a great many haskell users "out there", but if anyone does have any further comments, I would be interested to hear them!
 
Old 06-05-2012, 08:18 AM   #4
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Quote:
Originally Posted by Snark1994 View Post
Thank you very much my brain's far too used to imperative programming. I might give up and learn LISP before I approach Haskell...
Well, when I once started (mh, I tried to start) with functional programming I took a look at Haskell, but then moved to Lisp. I'm still learning Lisp. But I have great difficulties to learn the "functional thinking".
I would recommend to read the newsgroup comp.lang.lisp
There are many books about Common-Lisp, many of them also online available, for example Peter Seibel's "Practical Common Lisp" http://www.gigamonkeys.com/book/
If you're familiar with Perl you may also take a look at "Higher Order Perl" http://hop.perl.plover.com/ which is also available as PDF for free download.

Markus

Last edited by markush; 06-05-2012 at 04:39 PM. Reason: typo
 
1 members found this post helpful.
Old 06-05-2012, 04:15 PM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632

Original Poster
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Originally Posted by markush View Post
Peter Seibel's "Practical Comman Lisp" http://www.gigamonkeys.com/book/
Working through this one now... Thanks for the suggestion, it's just the kind of thing I wanted for Haskell, something that was a bit more real-world-example-based.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash: how does this $[ ] code snippet work? catkin Programming 2 09-09-2009 12:34 PM
XML style guidelines MALDATA Programming 6 03-26-2009 04:54 PM
Posting guidelines... Answering guidelines? itsjustme LQ Suggestions & Feedback 14 07-22-2005 01:08 PM
How do I compile a code snippet. to test my box?? _Mhz Linux - Newbie 4 07-10-2002 11:03 AM
calendar-related code snippet request Citizen Bleys Programming 2 12-11-2001 12:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration