0
votes

I'm working on a way of representing memory in Haskell that looks like this...

data MemVal = Stored Value | Unbound
          deriving Show

type Memory = ([Ide],Ide -> MemVal)

As an Identifier is called its added to the list of Identifiers. If an error occurs in the program I want to be able to recall the identifiers used up to date. So far I have this...

display :: Memory -> String
display m = "Memory = " ++ show (map (snd m) (fst m)) ++ " "

But was wondering if there were a way to map the name of the identifier to (fst m) as well as the function (snd m) so the output will be similar to...

Memory = [sum = stored Numeric 1, x = stored Boolean true]

Thank you.

2
Can you create a minimal working example? For example, what is Ide? What is Value? - pdexter
How about show [(ident, snd m ident) | ident <- fst m] - n. 1.8e9-where's-my-share m.
Value is data Value = Numeric Integer | Boolean Bool | ERROR deriving Show and Ide is either a Stored Value or Unbound defined by the MemVal - Matthew Rae

2 Answers

1
votes

You probably want something like this

display :: Memory -> String
display (ides, mem) = 
   "Memory = [" ++ unwords (map (\x -> x ++ "=" ++ mem x) ides) ++ "]"
1
votes

I'm guessing this is what you are after:

import Data.List (intercalate)

display (Memory ids f) = "Memory = [" ++ (intercalates ", " assigns) ++ "]"
  where assigns = [ show i ++ " = " ++ show (f i) | i <- ids ]

Here assigns is a list like:

[ "sum = stored Numeric 1", "x = stored Boolean true", ...]

and intercalate ", " assigns joins the strings together.

I've used destructuring to avoid having to refer to fst ... and snd ...