veganism.social is one of the many independent Mastodon servers you can use to participate in the fediverse.
Veganism Social is a welcoming space on the internet for vegans to connect and engage with the broader decentralized social media community.

Administered by:

Server stats:

240
active users

#haskell

31 posts25 participants1 post today

#haskell help !

Using a `lookup` inside a State monad function .

I can't do

lu <- lookup place maze

I have to handle Nothing myself after

let lu = lookup place maze

Q - What's the idiomatic way?

---

visit :: [(String,[String])] -> String -> State [String] ()
visit maze place = do
s <- get
if elem place s
then do
return ()
else do
put (place : s)
let lu = lookup place maze
case lu of
Nothing -> return ()
Just ns -> forM_ ns (visit maze)

Learn You A Haskell for Great Good does one thing well I imho.

It discussed a random number generator to introduce the idea of internal state compared to output value.

When you pull a random number, the state of the generator changes, so that the next pulled value is different.

And this works really well to explain the State Monad.

(I'm still learning, and wading through terrible and good explanations)

#haskell

the following took me 3 days to get right - it passes the mooc testing

but is there a better "model solution" way than my combining an fmap inside a >>= operation ?

looks a bit clunky and non-idiomatic

---

countAndLog :: Show a => (a -> Bool) -> [a] -> Logger Int
countAndLog f [] = return 0
countAndLog f (x:xs)
| f x = Logger [show x] 1 >>= (\n -> fmap (+1) (countAndLog f xs))
| otherwise = countAndLog f xs

---

1/2

Replied in thread

@ramin_hal9001

my beginner brains wants to see

let b = f a
in Logger (something) b

because f is applied to a to get b

---

also the la++lb is confusing me because la is supplied as an argument but I can't see where lb comes from? there is no lb = ...

Replied in thread

@ramin_hal9001

actually an hour later and I'm still not sure, so I went back to an earlier example in the #haskell course which I also don't understand

--

(#>) :: Logger a -> (a -> Logger b) -> Logger b
Logger la a #> f = let Logger lb b = f a -- feed value to next step
in Logger (la++lb) b -- bundle result with all messages

--

here again I'm seeing `let` but in an unfamiliar way

let "expression = epression" instead of let "variable = expression" .. I don't get it

new #haskell puzzle!

I'm trying to read and understand this code:

---

instance Monad Logger where
return x = Logger [] x
Logger la a >>= f = Logger (la++lb) b
where Logger lb b = f a

---

I'm familiar with simple `where` eg x = y where y = z + 1

but in this example the `y` in `where y =` doesn't actually appear in the preceding code.

hmmm...

---

does it patten match (la ++ lb) to lb in the where block? this doesn't make sense unless the two `lb` are not the same.

Continued thread

if an index is out of scope then I need to return Nothing, and my safeIndex does that

in the sum itself, I know I need recursion starting with

safeIndex xs i

and I know I need to chain this using >>= to a function that does the addition - that's where I'm stuck

I also know somewhere there must be a tail selectSum xs is

(we haven't done Applicative's yet in this #haskell course so can't use those )

2/2

struggling with this #haskell exercise

given a list of numbers, and a list of indices, I need to find the sum of the indexed numbers

I need to use the >>= operator and the safeIndex function

I can't see how to do the recession

----

selectSum :: Num a => [a] -> [Int] -> Maybe a
selectSum _ [] = Just 0
selectSum xs (i:is) = safeIndex xs i >>= ???? selectSum xs is

safeIndex :: [a] -> Int -> Maybe a
safeIndex xs i
| i >= 0 && i < length xs = Just (xs !! i)
| otherwise = Nothing

--

1/2

Replied in thread

@ivan I added #crem from #haskell then dropped it when I realized that the wonderful module basically requires #dependenttypes via #singletons. I honestly adore the idea but the type system needs to do too many gymnastics until we can get a type system like #Idris2 or #Granule.

I am the type of person to run before I walk so it took one #pragmatic motherfucker to make that decision without feeling like I had given up something beautiful and elegant.

🌗 Haskell 中的封裝數據支持
➤ 使用類型系統實現高效數據處理的新方法
arthi-chaud.github.io/posts/pa
本文介紹了 Haskell 語言中新型的“封裝數據”格式,它允許在不進行序列化和反序列化步驟的情況下直接使用二進制數據,從而提高性能。相較於傳統的序列化方式,封裝數據避免了指針操作和不必要的數據複製,尤其在樹狀結構的遍歷中表現更佳。作者介紹了 `packed-data` Haskell 函式庫,它利用 Template Haskell 和類型系統實現了封裝數據的編碼、解碼和遍歷功能,無需修改編譯器。
+ 感覺這個函式庫對於需要處理大量數據的應用來說很有用,比如數據庫或網絡服務。
+ 雖然概念有點複雜,但是如果能真正提升性能,那麼學習曲線是值得的。
#Haskell #數據序列化 #性能優化

Arthi-chaud · Packed Data support in HaskellPacked Data x Haskell = Portable(Type-safety + performance)