29 August 2009

Haskell agus Tionscadal Euler

Tá mé ag foghlaim Haskell anois. Is teanga suimiúil ríomhaire é, teanga feidhmeána ("feidhmeán" sa chiall matamaitice). Agus tháinig mé ar Tionscadal Euler le déanaí, suíomh idirlín lán de fadhbanna beaga matamaitice. Is maith iad mar chleachtadh chlárú ríomhaire Seo mo réiteach do uimhir a haon:

multipleOf m n = m `mod` n == 0
chosenNumbers = filter (\x -> x `multipleOf` 3 || x `multipleOf` 5) [1..999]
main = print ( sum chosenNumbers )

Ach nuair a léigh mé an fóram, chonaic mé an chaoi is fearr liom:

sum [n | n <- [1..999], n `mod` 5 == 0 || n `mod` 3 == 0]

I'm learning Haskell now. It's a functional programming language (functional in the mathematical sense). And I came across Project Euler recently, which has short maths problems that are great for practising programming. The first bit of code is my own solution; the second is a much better one that I found on the forum.

No comments: