How to run a for loop in Haskell

There’s no such a thing as a for loop in Haskell. To apply a function to each element in a list, you can either use map or a list comprehension. Since you already have a list comprehension (which currently does not do anything), let’s just use that: mainfun xs = [helpfun 2 x | x … Read more

Histogram in Haskell [closed]

We can start by generating ranges required, and then map count function onto ranges to get list of partial functions, which finally can be applied on the given list. ranges lim n = takeWhile (\(x,y) -> x < lim) [(k*n, (k+1)*n – 1) | k <- [0..]] count (l,r) = length.filter (\x -> l <= … Read more

Haskell, tuple (double, string) [closed]

You can implement it using mapM_/traverse_ or the flipped argument versions: forM_/for_. I prefer for_ since it looks more like the “enhanced for-loop” from languages like Java. import Data.Foldable (for_) myPutStr :: [(Double,String)] -> IO () myPutStr vals = do for_ vals $ \(num, str) -> do putStr str putStr “: ” print (num * … Read more

Haskell \p -> not takeWhile

\p -> square p <= n It’s an unnamed (anonymous) function that accepts variable p, squares it and compares result with n. all (\p -> not $ divideBy p n) testingSlice checks that anonymous function returns true value for each element in testingSlice. Read about lambdas.

battleships game – Haskell [closed]

You need to transform the input grid into the output grid by mapping a function over each cell of the grid. The mapping function is going to need the coordinates of the cell and the content of the cell. The coordinates are needed to see if the other player fired at that location, and the … Read more