Lists of Lists

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
20 messages Options
Reply | Threaded
Open this post in threaded view
|

Lists of Lists

zell_ffhut
Just wondering if anyone can help me with a Haskell problem im having.

How would I get the value of, lets say, the 9th object in ["4179355"," 567412"] ?

(in this case, that should return 6)

Any help/adivce would be great :)
Reply | Threaded
Open this post in threaded view
|

Re: Lists of Lists

Jens Fisseler
> How would I get the value of, lets say, the 9th object in ["4179355","
> 567412"] ?
>
> (in this case, that should return 6)
>
> Any help/adivce would be great :)

Just concatenate the list elements and index the resulting list, but
note that list indices start from 0. Nevertheless I would say this is a
strange way to index such a list.

Regards,

        Jens
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Lists of Lists

Daniel Fischer-4
In reply to this post by zell_ffhut
Am Mittwoch, 8. März 2006 10:51 schrieb zell_ffhut:

> Just wondering if anyone can help me with a Haskell problem im having.
>
> How would I get the value of, lets say, the 9th object in ["4179355","
> 567412"] ?
>
> (in this case, that should return 6)
>
> Any help/adivce would be great :)
> --
> View this message in context:
> http://www.nabble.com/Lists-of-Lists-t1245394.html#a3297506 Sent from the
> Haskell - Haskell-Cafe forum at Nabble.com.
>
Use concat and (!!) ?

Cheers,
Daniel

--

"In My Egotistical Opinion, most people's C programs should be
indented six feet downward and covered with dirt."
        -- Blair P. Houghton

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re[2]: Lists of Lists

Bulat Ziganshin-2
In reply to this post by Jens Fisseler
Hello Jens,

Wednesday, March 8, 2006, 2:31:36 PM, you wrote:

>> How would I get the value of, lets say, the 9th object in ["4179355","
>> 567412"] ?
JF> Just concatenate the list elements and index the resulting list, but

btw, due to the lazy evaluation time required to perform this
operation will be proportional to used index, not to the size of whole
catenated list


--
Best regards,
 Bulat                            mailto:[hidden email]

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Lists of Lists

Ketil Malde-2
In reply to this post by zell_ffhut
zell_ffhut <[hidden email]> writes:

> Just wondering if anyone can help me with a Haskell problem im having.

> How would I get the value of, lets say, the 9th object in ["4179355","
> 567412"] ?

You mean the ninth character in the concatenation of the list of
strings?  Concatenate the strings, and select the ninth character?

Use 'concat' '!!', and probably '.' and '9' as well.

-k
--
If I haven't seen further, it is by standing in the footprints of giants

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Lists of Lists

zell_ffhut
I've tried using the Concat and ++ functions, but to no avail so far.

Imagine the strings are set out in a 9x9 grid type way, and i have to find the value of a set position given 2 gird values.

> getCharFromGrid (row,col) g = concat g !!(row * 9) + col

The decleration is

> getCharFromGrid :: Position -> Grid -> Char

Any ideas why its not working?
Reply | Threaded
Open this post in threaded view
|

Re: Lists of Lists

Sebastian Sylvan
On 3/8/06, zell_ffhut <[hidden email]> wrote:

>
> I've tried using the Concat and ++ functions, but to no avail so far.
>
> Imagine the strings are set out in a 9x9 grid type way, and i have to find
> the value of a set position given 2 gird values.
>
> > getCharFromGrid (row,col) g = concat g !!(row * 9) + col
>
> The decleration is
>
> > getCharFromGrid :: Position -> Grid -> Char
>
> Any ideas why its not working?

Well, did you try:

getCharFromGrid (row,col) g = concat g !! (row * 9 + col)

(what you wrote would extract the col*9'th element and then add col to
that element)

Or how about:

getCharFromGrid (row,col) g = g !! row !! col

/S
--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Lists of Lists

zell_ffhut
Thank you, It's working as planed now

Trying to do a function now that changes the value of an element of the list. In programming languages i've used in the past, this would be done somthing like -

> changeValue x i [xs] = [xs] !! i = x

where x is the value to change to, i is the index of the value to change, and [xs] is the list.

This however, dosen't work in Haskell. How would this be done in Haskell?
Reply | Threaded
Open this post in threaded view
|

Re: Lists of Lists

Thomas Davie

On 8 Mar 2006, at 14:21, zell_ffhut wrote:

>
> Thank you, It's working as planed now
>
> Trying to do a function now that changes the value of an element of  
> the
> list. In programming languages i've used in the past, this would be  
> done
> somthing like -
>
>> changeValue x i [xs] = [xs] !! i = x
>
> where x is the value to change to, i is the index of the value to  
> change,
> and [xs] is the list.
>
> This however, dosen't work in Haskell. How would this be done in  
> Haskell?

Put simply it isn't.

One of the percepts of a functional language is that variables are  
bound, not set - once a variable has a value it has that value  
forever.  What you want to do is return a new list, that looks like  
the old one, but has one value changed

changeValue x 0 (y:ys) = (x:ys)
changeValue x n (y:ys) = y:(changeValue x (n-1) ys)

Bob
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Lists of Lists

Jens Fisseler
In reply to this post by zell_ffhut
> > changeValue x i [xs] = [xs] !! i = x
>
> where x is the value to change to, i is the index of the value to change,
> and [xs] is the list.
>
> This however, dosen't work in Haskell. How would this be done in Haskell?

Think about what parts of the list you can reuse, how you can define
those parts and put them together.

You probably want to split the list at the index. 'splitAt' is the
function to use for this, as it will give you the resulting prefix and
suffix. The head of the suffix is the element you want to replace. So
you create a new list by appending the unaltered prefix, the new element
and the 'tail' of the suffix.

Regards,

        Jens
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Lists of Lists

zell_ffhut
Stunning. That really helps

Different thinking to what im used to :)

Can i now use this function to find and replace values in Lists of Lists?
Reply | Threaded
Open this post in threaded view
|

Re: Lists of Lists

zell_ffhut
Im afraid im baffled again!

Im now trying to add a char to a string of strings (eg - ["434233434" "444929192" "909313434"]

Im sure i can use my previous function to help me achive this, but i can't seem to get it workinging

> charToGrid :: Char -> Position -> Grid -> Grid
> charToGrid c (row,col) g = concat g (changeValue c(row*9 + col))

Im not sure i should be using concat, as i have to return a grid as it was given, just with the added char.
Reply | Threaded
Open this post in threaded view
|

Re: Lists of Lists

Bugzilla from robdockins@fastmail.fm

On Mar 8, 2006, at 1:29 PM, zell_ffhut wrote:

> Im afraid im baffled again!
>
> Im now trying to add a char to a string of strings (eg - ["434233434"
> "444929192" "909313434"]
>
> Im sure i can use my previous function to help me achive this, but  
> i can't
> seem to get it workinging
>
>> charToGrid :: Char -> Position -> Grid -> Grid
>> charToGrid c (row,col) g = concat g (changeValue c(row*9 + col))
>
> Im not sure i should be using concat, as i have to return a grid as  
> it was
> given, just with the added char.

As before, the idea is to create a new list with the changes you  
want, only now you have a list "two levels deep".  So the first thing  
to do is to pick out the sublist (row) you want to "change" and  
create a new changed sublist (row), and then rebuild your grid.  Try  
this, it may get you started:

updateList :: (a -> a) -> Int -> [a] -> [a]
updateList f i l = begin ++ (f x : end)
   where (begin, x : end) = splitAt i l


BTW, lists aren't very good for these kinds of manipulations.  If you  
really need an indexable, mutable data structure, try one of  
Data.Array.*


Rob Dockins

Speak softly and drive a Sherman tank.
Laugh hard; it's a long way to the bank.
           -- TMBG

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Lists of Lists

Udo Stenzel
In reply to this post by zell_ffhut
zell_ffhut wrote:
> Imagine the strings are set out in a 9x9 grid type way, and i have to find
> the value of a set position given 2 gird values.
>
> > getCharFromGrid (row,col) g = concat g !!(row * 9) + col

This isn't by chance evolving into the world's ugliest sudoku solver?
Are you sure, you don't want to use a FiniteMap?


Udo.
--
Slous' Contention:
        If you do a job too well, you'll get stuck with it.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Lists of Lists

zell_ffhut
In reply to this post by Bugzilla from robdockins@fastmail.fm
Could you explain what the function does.. I can't seem to peice it together.
Reply | Threaded
Open this post in threaded view
|

Re: Lists of Lists

Bugzilla from robdockins@fastmail.fm

On Mar 8, 2006, at 2:27 PM, zell_ffhut wrote:
> Could you explain what the function does.. I can't seem to peice it  
> together.

It takes three things 1) a function 2) an index and 3) a list.  It  
finds the nth element of the list, applies the function to it and  
then returns a new list containing the new element in the same  
position.  It dies with an error message if you index past the end of  
the list.

e.g.

updateList (\x -> x + 10) 3 [0,1,2,3,4,5] == [0,1,2,13,4,5]


Rob Dockins

Speak softly and drive a Sherman tank.
Laugh hard; it's a long way to the bank.
           -- TMBG


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Lists of Lists

zell_ffhut
I see, that seems to make sence.

Im still unsure of how to do that with the specification set to me

> charToGrid :: Char -> Position -> Grid -> Grid
Reply | Threaded
Open this post in threaded view
|

Re: Lists of Lists

zell_ffhut
Thanks for all the help i've recived through e-mails, but im still stuck on this

Anyone want to save the day? :(
Reply | Threaded
Open this post in threaded view
|

Re: Lists of Lists

zell_ffhut
Last attempt, as its due in a couple of hours

Here's what i have so far..

> charToGrid :: Char -> Position -> Grid -> Grid
> charToGrid c [] (row,col) xs = xs
> charToGrid c (row,col) xs = (changeValue c (concat xs (row*9 + col)))

Using changeValue -

changeValue x 0 (y:ys) = (x:ys)
changeValue x n (y:ys) = y:(changeValue x (n-1) ys)

Would really appritiate any help
Reply | Threaded
Open this post in threaded view
|

Re: Lists of Lists

Dmitry V'yal
zell_ffhut ?????:

> Last attempt, as its due in a couple of hours
>
> Here's what i have so far..
>
>> charToGrid :: Char -> Position -> Grid -> Grid
>> charToGrid c [] (row,col) xs = xs
>> charToGrid c (row,col) xs = (changeValue c (concat xs (row*9 + col)))
>
> Using changeValue -
>
> changeValue x 0 (y:ys) = (x:ys)
> changeValue x n (y:ys) = y:(changeValue x (n-1) ys)
>
> Would really appritiate any help
> --
> View this message in context: http://www.nabble.com/Lists-of-Lists-t1245394.html#a3315187
> Sent from the Haskell - Haskell-Cafe forum at Nabble.com.
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>

You can easily do it using updateList as Robert Dockins proposed.

updateList :: (a -> a) -> Int -> [a] -> [a]
updateList f i l = begin ++ (f x : end)
  where (begin, x : end) = splitAt i l

changeGrid :: Char -> (Int,Int) -> Grid -> Grid
changeGrid c (row,col) grid = updateList updateRow row grid
    where updateRow l = updateList (const c) col l

Here we replace a row with a new one, using a helper function updateRow which,
given the row, changes one symbol at position col in it. updateList is used
twice here. First it's applied to outer list and then to inner one. It is
possible because of higher-order nature of updateList. It's behavior can be
changed by suppling an appropriate function f as it's first argument.
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe