Haskell: manipulate data type of a defined class

I tried to comment everything to make my code very clear to everyone. Please read the comment to better understand.What I have so far, is a Team made up of players, and those players will eventually get a position: defender, middlefield, or striker. For example, in:let ykTeam = Team(Map.fromList [(1, [ 2, 3]), (2, [1]), (3, [1, 4]), (4, [3])])the item (1, [ 2, 3]) means that player 1 can also play 2 or 3 (not so important right now).Question : From the code shown below, I want to write the initPlan function, but I'm struggling to manipulate ...Read more

functional programming - How to make a change within nested data types in Haskell

I got this:type ID = Intdata Bank = Bank [(ID, Account)] deriving Showdata Account = Account { balance :: Int , owner :: Client } deriving Showdata Client = Client { name :: String , surname :: String , address :: String } deriving Showmy task is, to write a function credit :: Int -> ID -> Bank -> Bankthat Adds the specified amount of Money to the specified amountAccount.I do not know how can i actually do that, because these are data types.....Read more

Haskell - Reading entire Lazy ByteString

Context: I have a function defined in a library called toXlsx :: ByteString -> Xlsx (that ByteString is from Data.ByteString.Lazy)Now to do certain operations I've defined certain functions that operate on the same file, thus I would like to open, read and convert to Xlsx the file once and keep it in memory to operate with it.Right now I'm reading the file as bs <- Data.ByteString.Lazy.readfile file and at the end doing Data.ByteString.Lazy.length bs 'seq' return value.Is there any way to use this function and keep the file in memory as a...Read more

Group consecutive elements of specific value in Haskell

I have a list of numbers in haskell and I want to perform the following transformation:When an element is not equal to 1, just append this element on the new listWhen an element is equal to 1 append to the list a sublist which contains all the consecutive 1s and then proceed with the next character that is not equal to 1To make the requirement clear I am posting an example:Input: [1,2,3,1,1,1,2,3,4,2,1]Expected Output: [[1] , 2 , 3 , [1,1,1] , 2 , 3 , 4 , 2 , [1]]Important Notice: The elements that are not equal to 1 are not added in a sublistI...Read more

Parse error at end of "let" in Haskell file

In this Haskell code, what error can you see at the end of "b2"? I believe there is no error, but this code in the file Q3.hs:let idPnumber a1 b2= take 3 a1 == take 3 b2gives the error:Q3.hs:1:45: error: parse error (possibly incorrect indentation or mismatched brackets) |1 | let idPnumber a1 b2= take 3 a1 == take 3 b2 | ^...Read more

haskell - Is it possible to expand elem to support a -> [[a]] -> Bool?

I have a polyomino problem that requires placing polyominos onto a board.I am using the following type to support this.type Block = (Int, Int)type Polyomino = [Block]type Board = [Polyomino]Part of my solution is a function that checks if any blocks of a polyomino have already been placed in the board.unPlaced :: Polyomino -> Int -> Int -> Board -> BoolunPlaced _ _ _ [] = TrueunPlaced ((a, b):cs) x y b = --check if in Board(x and y are the offsets of the current polyomino)I know I can use elem to check in each polyomino in the board...Read more

permutation - flatten a tree with a list of subtrees in Haskell

I would like to flatten a tree which looks like this:> data Tree a = Leaf a> | Fork a [Tree a]> | Root [Tree a]possible example:Root [Fork 'a' [Fork 'b' [Leaf 'c'],Fork 'c' [Leaf 'b']],Fork 'b' [Fork 'a' [Leaf 'c'],Fork 'c' [Leaf 'a']],Fork 'c' [Fork 'a' [Leaf 'b'],Fork 'b' [Leaf 'a']]]should become["abc","acb","bac","bca","cab","cba"]To explain why:I try to build kind of a permutation Tree. I wrote a function permute :: String -> Tree Char to visualize all possible permutations of an string into a Tree. B...Read more

polymorphism - Implementing a language in Haskell: dynamic typing for polymorphic functions?

I’m writing a programming language using Haskell as the host language. (The parser, grammar, runtime, etc. for my language are all written in Haskell.) Depending on what’s written in a program in my PL, in my language’s runtime, I want to apply Haskell functions of different types dynamically (their types are known at runtime) to Haskell values of different types, whose types are known at runtime. I want to apply these functions in a generic fashion, without having to case on every possible type. Basically I want this part of my language’s runt...Read more

Pattern Matching - Prolog vs. Haskell

This is not a homework question, rather an exam study guide question. What is the difference between pattern matching in Prolog Vs Haskell?I've done some research and reading up on the theories behind them doesn't really give me a solid understanding between the two. I read that in Prolog, pattern matching is different because it has the ability to unify variables and thus be able to deduce through resolution and spit out the possible answereg ?- [a,b] = [a,X] X = bNow I'm not sure how to display pattern matching in Haskell. I know that the s...Read more

Haskell random IO to Int

I recently picked up Haskell and the syntax is really confusing to me. I am trying to get a random number between 0 and 51 usingrandomRIO (0, 51)But I dont know how to make this an actual Int (now its type IO a). I believe you are supposed to be able to do:gen <- randomRIO (0, 51)and then gen is supposed to be an Int but I have to do this in a do block? How do I create the function that takes this and return an Int.From googeling I'm afraid that there is something basic that I do not understand. I am completely lost....Read more