This is a common misunderstanding:

match list1 with

| a1::t1 -> ...

will match all lists with at least one element, and bind the name 'a1' to the head of the list and 't1' to the tail of the list. The previous value of 'a1' gets shadowed.

You want something more like

match list1 with

| h1::t1 when h1 = a1 -> ...

which will test if the head of the list has the same value as a1.

(Don't spend the whole day looking when you can just ask and possibly get a quick response!)

By on 5/29/2009 6:19 PM ()

So the program get form

let rec remove a1 list1 =

match list1 with

| h1::t1 when h1=a1->t1;

| h1::t1-> remove a1 t1 ;

| []->list1;;

let rec printInt abc =

match abc with

| a::b-> printfn "%d," a;printInt b;

| []-> printfn "stop";;

let removing a =

let b=remove a [1;3;2;4];

printInt b;;

removing(2);

As answer I get b=[4]. not [1;3;4]

By on 5/29/2009 7:26 PM ()

On this line of 'remove'

| h1::t1-> remove a1 t1 ;

you are throwing away the head value h1. You want

| h1::t1-> h1 :: (remove a1 t1) ;

That is, for example, the answer to

remove 2 [1;3;2;4]

should be

1 :: (remove 2 [3;2;4])

That is, you want to keep the element at the front, but recurse into the rest of the list to find the element you intend to remove.

By on 5/29/2009 7:53 PM ()

I will try your sugestion. Whar you have written about match with list is clear for me. However this works when variables in expression a::b do not have value. If imutable variable at match changes value this is a bug. I cannot write

| a::b if a=a

so I need to change variable.

and if there would be information how program flows this question may be not appear.

By on 5/29/2009 7:07 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper