[Haskell-cafe] Simple Table Update

Paul Keir pkeir at dcs.gla.ac.uk
Wed Oct 8 16:12:45 EDT 2008


It brings tears to my eyes to see such a beautiful function ;) You've hit the nail on the head Ryan. The new version of my second previous code snippet now no longer requires "fixpu*", and is:

typeCheckAST (Program pus) = Program $ snd $ mapAccumL f [] pus

  where f st pu = (st ++ [("",[])],pu)

Many thanks,
Paul

-----Original Message-----
From: Ryan Ingram [mailto:ryani.spam at gmail.com]
Sent: Wed 08/10/2008 18:03
To: Paul Keir
Cc: haskell-cafe at haskell.org
Subject: Re: [Haskell-cafe] Simple Table Update
 
Prelude> :t Data.List.mapAccumL
Data.List.mapAccumL :: (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])

I'm sure you can fill in the details :)

  -- ryan

2008/10/8 Paul Keir <pkeir at dcs.gla.ac.uk>:
> Hi,
>
> I'd like to create a new list based on an original list, using information
> from a second (symbol) list. That second list should be updated as each
> element in the new list is added. I've been using map a lot, but that's not
> an option here, and I'm having trouble obtaining a good recursive structure
> for my function.
>
> The minimal "fixpus" function below shows the problem. I have "sts" on both
> sides of the ":" list construct, but I'd like the second "sts" to be a
> version modified by the where (singular) function, "fixpu".
>
> fixpus :: [ProgUnit] -> [ProgUnitSymbolTable] -> [ProgUnit]
> fixpus []       _  = []
> fixpus (pu:pus) sts = fixpu pu sts : fixpus pus sts
>
>   where fixpu pu sts = pu
>
> (Below) I tried making "fixpu" return a tuple, and then use "fst", "snd" and
> "let", but I think it looks strange. I know it's quite basic, but I'd like a
> strong foundation for what's likely to become a medium-scale project. Can
> anyone offer advice?
>
> fixpus :: [ProgUnit] -> [ProgUnitSymbolTable] -> [ProgUnit]
> fixpus []       _  = []
> fixpus (pu:pus) sts = let a = (fixpu pu sts) in fst a : fixpus pus (snd a)
>
>   where fixpu pu sts = (pu,sts ++ [("",[])])
>
> Regards,
> Paul
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20081008/824bc77d/attachment.htm


More information about the Haskell-Cafe mailing list