mathstodon.xyz is one of the many independent Mastodon servers you can use to participate in the fediverse.
A Mastodon instance for maths people. We have LaTeX rendering in the web interface!

Server stats:

3K
active users

#haskell

67 posts39 participants1 post today
Continued thread

I've tried debugging by pen and paper to work through what foldr and the helper function does step by step using the definition from the online course:

foldr :: (a -> b -> b) -> b -> [a] -> b
foldr f y [] = y
foldr f y (x:xs) = f x (foldr f y xs)

----

f 2 [1,1] = f 2 ( f 1 [1])
= f 2 ( f 1 ( f 1 [] ) )
= f 2 ( f 1 [1] )
= f 2 ( [1,1] )
= [2,1]

... ok I think I found the bug

so my logic is wrong.

stuck on a #haskell puzzle

aim return a list of all occurrences of the largest Int in a list

my solution has a bug

[2,1,1] it returns [2,1] not [2]

--------

-- Examples:
-- largest [] ==> []
-- largest [1,3,2] ==> [3]
-- largest [1,3,2,3] ==> [3,3]

largest :: [Int] -> [Int]
largest xs = foldr largestHelper [] xs

largestHelper :: Int -> [Int] -> [Int]
largestHelper x [] = [x]
largestHelper x (y:xr)
| x > y = x:xr
| x == y = x:y:xr
| otherwise = y:xr

--------

help!