For Programmers: Free Programming Magazines  


Home > Archive > Functional > May 2007 > what is wrong w\this









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 what is wrong w\this
JS_Y

2007-05-29, 7:06 pm

Hi
I am new to ml. i am trying to write a function which returns true if
2 of the elem which are next to each other in a given list are the
same and false if not. this is what i came up with, not working! any
help
thx
fun check (x,nil) = false
| check (x, (y :: ys)) = (

if (x = y) then true

else false;
fun fid nil = false
| fid (y :: ys) = ( check (y, (fid ys)) );

JS_Y

2007-05-29, 7:06 pm

& is there an easier way where i can do this using only one function ?

cane

2007-05-29, 7:06 pm

JS_Y wrote:
> & is there an easier way where i can do this using only one function ?


Maybe something like this:

fun fid [] = false
| fid [x] = false
| fid (x::y::ys) = if (x=y) then true else fid (y::ys);
Ivan Jager

2007-05-30, 10:06 pm

On 2007-05-29, cane <xxx@xxx.com> wrote:
> JS_Y wrote:
>
> Maybe something like this:
>
> fun fid [] = false
>| fid [x] = false
>| fid (x::y::ys) = if (x=y) then true else fid (y::ys);


Or if you want something shorter:
fun fid (x::y::ys) = x=y orelse fid (y::ys)
| fid _ = false

Ivan :)

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com