문제

I have this piece of code which calulates the hanoi movement and returns them as a list. This works so far.

fun hanoi(0,start,ziel) = []
  | hanoi(1,start,ziel) = [(1,start,ziel)]
  | hanoi(anz,start,ziel) = hanoi(anz-1,start,hilf(start,ziel)) 
                   @ ((anz,start,ziel)::hanoi(anz-1,hilf(start,ziel),ziel))
and hilf(start,ziel) = 6-(start+ziel);

Now my question is how can I check for the last pattern in which start and the goal (german: ziel) is the same? I tried something like this:

|hanoi(anz,start,ziel=start) = []

but it did not work. Seems like I am having problems understanding the concept of pattern matching fully. But I couldn't find much helpful ressources.

Thanks in advance!

도움이 되었습니까?

해결책

Generally speaking, you don't check for this, because start=ziel is not a pattern -- patterns are (for the most part) values or special patterns like _ or variable names, while start = ziel is an expression that is not fully evaluated.

Instead, pattern match to extract start and ziel, and then you can either case on or use an if statement to do different things depending on whether or not start = ziel.

For example:

| hanoi(anz, start, ziel) = if start = ziel
                            then ...
                            else ...

or

| hanoi(anz, start, ziel) = case start = ziel of
                              true => ...
                            | false => ...

These two options do the same thing -- which to use is a stylistic decision.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top