r/functionalprogramming • u/chakkramacharya • Mar 02 '24
Question Haskell, lookup over multiple data structures.
I am writing a toy program.. it takes a string say "tom" and splits it into individual characters and gives out the following data
t = thriving o = ornate m = mad here the adjectives thriving, ornate and mad are stored in a data structure as key value pairs eg: ('a' , "awesome")
The issue i have is when a string has the same characters, the same adjective gets repeated and i don't want repetitions.
eg:- if i give the name sebastian, the adjectives "serene" and "awesome" is repeated twice.. which i don't want..
It should select another adjective for the letters s and a ? How do i do that? Should i add more data structures? How do i move from one to another so as to avoid repetitions?
I am reproducing the code done till now below
-- Main.hs
module Main where
import qualified Data.Map as Map
-- Define a map containing key-value pairs of alphabets and their values
alphabetMap :: Map.Map Char String
alphabetMap = Map.fromList [
('a', "awesome"),
('b', "beautiful"),
('c', "creative"),
('d', "delightful"),
('e', "energetic"),
('f', "friendly"),
('g', "graceful"),
('h', "happy"),
('i', "innovative"),
('j', "joyful"),
('k', "kind"),
('l', "lovely"),
('m', "mad"),
('n', "nice"),
('o', "ornate"),
('p', "peaceful"),
('q', "quiet"),
('r', "radiant"),
('s', "serene"),
('t', "thriving"),
('u', "unique"),
('v', "vibrant"),
('w', "wonderful"),
('x', "xenial"),
('y', "youthful"),
('z', "zealous")
]
-- Function to look up a character in the map and return its value
lookupChar :: Char -> String
lookupChar char = case Map.lookup char alphabetMap of
Just val -> val
Nothing -> "Unknown"
-- Function to split a string into characters and look up their values
lookupString :: String -> [String]
lookupString str = map lookupChar str
main :: IO ()
main = do
putStrLn "Enter a string:"
input <- getLine
let result = lookupString input
putStrLn "Result:"
mapM_ putStrLn resultThanks in advance for helping out..
6
u/aaaaargZombies Mar 02 '24
If your alphabet map was
Map.Map Char [String]and you did a fold over the name and the map you could discard used adjectives from the map as you add them to the result list. Then you know they aren't being reused.You will need a larger and larger dictionary as your input grows though and it will fail eventually. This is always going to be the case if you want zero repetitions and have no limit on the input size.
If you don't care about the lengths of input and output matching you could just remove duplicates with nub
4
u/Ordinary-Price2320 Mar 02 '24
There are many ways to solve this problem. I'd consider a map of char, stack<string> type, where each pass over a certain character would pop another word from the stack. You'd need to ensure you don't run out of words. I also do not know Haskell, so the equivalent types may be called differently.
1
u/jecxjo Mar 06 '24
If all your data structures are structured the same i would do a
foldon the structures and azipandfmapover the data inside them to get aMap Char [String]The lookup process would return the new data structure with the used word removed. Then make it a recursive method and assuming you don't run out of words you'll not repeat any words.
2
u/XDracam Mar 02 '24
Here's a different approach: use the hash of the input as seed for a pseudorandom number generator. Then for every number generate a letter between 0 and 1 and multiply it by the length of the
[String]. Then take the value at that index.Note that indexing a single linked list runs in linear time in Haskell, so you might want to use an indexed collection if performance happens to be a concern. E.g. when there are more than 1000 words in each list.