When studying Haskell, take care to help with coding style?

I started learning hackell yesterday and I'm stuck in a problem. After a few different attempts, I thought that I had finally come here and would ask how to fix it. Also, feel free to criticize the way I have done it so far, so I can know which direction to go. Thank.

module Main where
main = putStrLn lastPrime
    where
        lastPrime :: String
        lastPrime = show(last(take 10001 primes))
        primes :: [Int]
        primes = [x| x <- [1..],length [a| a <- [1..lessen(x)], mod x a /= 0] == x - 2]
        lessen :: Int -> Int
        lessen a = ceiling(sqrt(a))
+3
source share
2 answers

To fix a type error, you want:

    lessen :: Int -> Int
    lessen a = ceiling (sqrt (fromIntegral a))

ahas a type Int, but sqrtexpects a floating-point type, and the easiest way to convert an integral type to another numeric type is fromIntegral.

+7
source

lessen :

length [a| a <- [1..lessen(x)], mod x a /= 0] == x - 2

( ) lessen x. , x - 2 . , , ( , , haskell , ).

, - O(n), , .

isPrime. :

module Main where
main = putStrLn lastPrime
    where
        lastPrime :: String
        lastPrime = show(last(take 10001 primes))
        isPrime x = length [a| a <- [1..lessen(x)], mod x a /= 0] == x - 2]
        primes :: [Int]
        primes = [x| x <- [1..], isPrime x]
        lessen :: Int -> Int
        lessen a = ceiling(sqrt (fromIntegral a))

. . , isPrime, . lessen x ( , 2, 1 ), , , , - x. any, :

isPrime x = not (any (\a -> mod x a == 0) [2 .. lessen x])
+4

Source: https://habr.com/ru/post/1751813/


All Articles