Home > Archive > Functional > April 2007 > List of Atom or List of List??
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
List of Atom or List of List??
|
|
| kheirandish.amin@gmail.com 2007-04-08, 8:02 am |
| how can detect some element of list is list o Atom??
for example
when i try head (head p1) where p1=[[1,2],2] return 1
but when try head (head p1) where p1=[1,2] it returns Error
| |
| kheirandish.amin@gmail.com 2007-04-08, 8:02 am |
| plz help me..it my project and i have just 2 hours..:(
| |
| kheirandish.amin@gmail.com 2007-04-08, 8:02 am |
| i want to replace on Atom by another in list..for example: replace 1 2
[1,2] => [2,2]
but sometime i have replace 1 2 [[1,2],2]=>[[2,2],2]
i want to detect if first element of list is list then recurcively
call head on that element..
is there any solutions??
| |
| Steve Schafer 2007-04-08, 7:03 pm |
| On 8 Apr 2007 02:46:25 -0700, kheirandish.amin@gmail.com wrote:
>how can detect some element of list is list o Atom??
(list? 42) --> #f
(list? '(42 37)) --> #t
Steve Schafer
Fenestra Technologies Corp.
http://www.fenestra.com/
| |
| kheirandish.amin@gmail.com 2007-04-08, 7:03 pm |
| in Haskell plz, not Scheme
| |
| Lauri Alanko 2007-04-08, 7:03 pm |
| In article <1176025585.229021.233310@w1g2000hsg.googlegroups.com>,
<kheirandish.amin@gmail.com> wrote:
> how can detect some element of list is list o Atom??
What is an atom? That is term is not commonly used with Haskell.
> for example
> when i try head (head p1) where p1=[[1,2],2] return 1
> but when try head (head p1) where p1=[1,2] it returns Error
It doesn't _return_ an error, but your expression fails to type-check.
Haskell's lists are homogeneous, i.e. all the elements are of the same
type. So either _all_ elements are lists (in which case the list of
lists has type [[a]] for some type a), or none of them are (in which
case the list has type [b] for some type b which is not a list type).
In any case, there is no run-time check to be made.
Perhaps you should tell what exactly you are trying to do?
Regards,
Lauri
| |
| kheirandish.amin@gmail.com 2007-04-08, 7:03 pm |
| for example:
i want to replace all 'a' by 'b' in list L..
['a','b','c','d'] => ['b','b','c','d']
[['a','b','c','d'] ]=>[['b','b','c','d'] ]
[[['a','b']] ,[['c','d']],[['a','d']]]=>[[['b','b']] ,[['c','d']],
[['b','d']]]
its correct that all elements of list must be same,but how i can
detect type of elemets..
perhaps [a] or [[a]] or nested list by any optional deep..
| |
| Lauri Alanko 2007-04-08, 7:03 pm |
| In article <1176055984.894137.172670@q75g2000hsh.googlegroups.com>,
<kheirandish.amin@gmail.com> wrote:
> for example:
> i want to replace all 'a' by 'b' in list L..
> ['a','b','c','d'] => ['b','b','c','d']
> [['a','b','c','d'] ]=>[['b','b','c','d'] ]
> [[['a','b']] ,[['c','d']],[['a','d']]]=>[[['b','b']] ,[['c','d']],
> [['b','d']]]
Are you _sure_ you really need your function to be polytypic like
this? If you only need to deal with lists and characters, then what
you want can be achived in plain Haskell 98 with some type class
trickery. However, it is non-trivial, and if this is a programming
exercise, then I strongly suspect that you have somehow misunderstood
the requirements.
> its correct that all elements of list must be same,but how i can
> detect type of elemets..
Type classes allow a function's behavior to depend on the type of its
parameter. You could define a Replacable:
class Replacable a where replace :: Char -> Char -> a -> a
and its instances:
instance Replacable Char where ...
instance Replacable a => Replacable [a] where ...
but as I said, I doubt this is what you really need.
Lauri
| |
| kheirandish.amin@gmail.com 2007-04-08, 7:03 pm |
| THIS IS MY PROBLEM:
NOTICE TO THE LAST LINE.THX :)
Write a function with three parameters, two atoms and a list, (say
p1,
p2 and L) that returns the list, L, with all occurrences of the first
given atom, p1, replaced by the second one, p2. If P2 be nil, the
given atom should be deleted and the returned list cannot contain
anything in place of it, NO MATTER HOW DEEP IT OCCURRED IN THE LIST.
| |
| Jeff M. 2007-04-08, 7:03 pm |
| On Apr 8, 1:34 pm, kheirandish.a...@gmail.com wrote:
> THIS IS MY PROBLEM:
>
[...snip...]
For the love of God, please do your own homework!
| |
| Lauri Alanko 2007-04-08, 7:03 pm |
| In article <1176057250.291101.197750@n59g2000hsh.googlegroups.com>,
<kheirandish.amin@gmail.com> wrote:
> THIS IS MY PROBLEM:
>
> NOTICE TO THE LAST LINE.THX :)
>
> Write a function with three parameters, two atoms and a list, (say
> p1,
> p2 and L) that returns the list, L, with all occurrences of the first
> given atom, p1, replaced by the second one, p2. If P2 be nil, the
> given atom should be deleted and the returned list cannot contain
> anything in place of it, NO MATTER HOW DEEP IT OCCURRED IN THE LIST.
This still doesn't give any indication about what "atoms" or "nil"
are. Those are not common notions in Haskell. In Lisp, "nil" typically
means the empty list, but the assignment seems to imply that here nil
is an atom. Although in Lisp it's possible for a single value to be
both a list and an atom, in Haskell that is not a typical situation.
Are you sure that your assignment (or accompanying material) doesn't
specify what atoms and nil are?
Are you sure that you aren't meant to define your own datatype that
includes both atomic and list-like values?
Finally, are you sure that you're meant to solve this in Haskell and
not, say, Scheme? :)
Lauri
| |
| kheirandish.amin@gmail.com 2007-04-08, 7:03 pm |
| For the love of God, please do your own homework!
chashm ostad.sorry sure jeff but ..... :p
i know scheme is easier than haskell,cause it has "list?" function but
haskell is now in my kher.(there is no way)
nil is my own datatype .
atoms : 1::Int 'a'::Char
thanks for your attention. :)
| |
| Mark T.B. Carroll 2007-04-09, 8:03 am |
| kheirandish.amin@gmail.com writes:
(snip)
> NO MATTER HOW DEEP IT OCCURRED IN THE LIST.
Are you sure they don't just mean, no matter how late it occurs in the
list? That is, the 'depth' is how far 'along' the list it is, rather
than to what degree it's wrapped up in sublists?
-- Mark
| |
| Mark T.B. Carroll 2007-04-09, 8:03 am |
| kheirandish.amin@gmail.com writes:
> in Haskell plz, not Scheme
I should mention that comp.lang.haskell exists for Haskell-specific
questions. A much wider range of stuff is of interest on
comp.lang.functional so you do sometimes have to be more specific
here in phrasing questions.
-- Mark
| |
| Steve Schafer 2007-04-09, 7:03 pm |
| On 8 Apr 2007 12:36:12 -0700, kheirandish.amin@gmail.com wrote:
>nil is my own datatype .
>atoms : 1::Int 'a'::Char
Haskell is strongly typed, so as Lauri suggests, the problem you're
trying to solve normally never arises. And, in fact, you have to jump
through a couple of hoops just to create a function that will accept
both lists of "atoms" and lists of lists. So, I think we need to see a
more complete example of how you would use the function you're trying to
write. In particular, what do you expect the function signature to look
like?
Steve Schafer
Fenestra Technologies Corp.
http://www.fenestra.com/
| |
| kheirandish.amin@gmail.com 2007-04-09, 7:03 pm |
| how can i use exception handling in haskell??
is there any sample code??
for example a function that take an Integer as argument.but we call it
by pass a list as argument and then programm detect it and print
something...
Thanks...
| |
| George Neuner 2007-04-09, 7:03 pm |
| On 8 Apr 2007 11:34:10 -0700, kheirandish.amin@gmail.com wrote:
>Write a function with three parameters, two atoms and a list, (say
>p1,
>p2 and L) that returns the list, L, with all occurrences of the first
>given atom, p1, replaced by the second one, p2. If P2 be nil, the
>given atom should be deleted and the returned list cannot contain
>anything in place of it, NO MATTER HOW DEEP IT OCCURRED IN THE LIST.
>
The problem is written using Lisp terminology. Are you sure you are
supposed to be using Haskell to solve it?
George
--
for email reply remove "/" from address
| |
| Albert Y. C. Lai 2007-04-09, 7:03 pm |
| kheirandish.amin@gmail.com wrote:
> for example a function that take an Integer as argument.but we call it
> by pass a list as argument and then programm detect it and print
> something...
Correction: a function that take an Integer as argument.but we call it
by pass a list as argument and then compiler detect it and print
something...
Solution: You don't need to do anything. The compiler does it for free.
Enjoy!
| |
| Mark T.B. Carroll 2007-04-09, 7:03 pm |
| kheirandish.amin@gmail.com writes:
> how can i use exception handling in haskell??
http://haskell.org/ghc/docs/latest/...-Exception.html
http://haskell.org/ghc/docs/latest/...onad-Error.html
> is there any sample code??
Are you really sure you need them? For instance, you can implement
simple backtracking with the help of things like Maybe. Can you tell
us more about what you're actually trying to do?
For instance, http://en.wikibooks.org/wiki/Haskell/MonadPlus is full of
good stuff which, while not using exceptions, shows some stuff where in
other languages you might use exceptions.
> for example a function that take an Integer as argument.but we call it
> by pass a list as argument and then programm detect it and print
> something...
Er, such a programme wouldn't compile anyway. That's what static
typechecking is all about. Or are you trying to do something with
the Typeable class or suchlike?
-- Mark
|
|
|
|
|