Search Results

Search found 17 results on 1 pages for 'dmindreader'.

Page 1/1 | 1 

  • backtracking in haskell

    - by dmindreader
    I have to traverse a matrix and say how many "characteristic areas" of each type it has. A characteristic area is defined as a zone where elements of value n or n are adjacent. For example, given the matrix: 0 1 2 2 0 1 1 2 0 3 0 0 There's a single characteristic area of type 1 which is equal to the original matrix: 0 1 2 2 0 1 1 2 0 3 0 0 There are two characteristic areas of type 2: 0 0 2 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 3 0 0 And one characteristic area of type 3: 0 0 0 0 0 0 0 0 0 3 0 0 So, for the function call: countAreas [[0,1,2,2],[0,1,1,2],[0,3,0,0]] The result should be [1,2,1] I haven't defined countAreas yet, I'm stuck with my visit function when it has no more possible squares in which to move it gets stuck and doesn't make the proper recursive call. I'm new to functional programming and I'm still scratching my head about how to implement a backtracking algorithm here. Take a look at my code, what can I do to change it? move_right :: (Int,Int) -> [[Int]] -> Int -> Bool move_right (i,j) mat cond | (j + 1) < number_of_columns mat && consult (i,j+1) mat /= cond = True | otherwise = False move_left :: (Int,Int) -> [[Int]] -> Int -> Bool move_left (i,j) mat cond | (j - 1) >= 0 && consult (i,j-1) mat /= cond = True | otherwise = False move_up :: (Int,Int) -> [[Int]] -> Int -> Bool move_up (i,j) mat cond | (i - 1) >= 0 && consult (i-1,j) mat /= cond = True | otherwise = False move_down :: (Int,Int) -> [[Int]] -> Int -> Bool move_down (i,j) mat cond | (i + 1) < number_of_rows mat && consult (i+1,j) mat /= cond = True | otherwise = False imp :: (Int,Int) -> Int imp (i,j) = i number_of_rows :: [[Int]] -> Int number_of_rows i = length i number_of_columns :: [[Int]] -> Int number_of_columns (x:xs) = length x consult :: (Int,Int) -> [[Int]] -> Int consult (i,j) l = (l !! i) !! j visited :: (Int,Int) -> [(Int,Int)] -> Bool visited x y = elem x y add :: (Int,Int) -> [(Int,Int)] -> [(Int,Int)] add x y = x:y visit :: (Int,Int) -> [(Int,Int)] -> [[Int]] -> Int -> [(Int,Int)] visit (i,j) vis mat cond | move_right (i,j) mat cond && not (visited (i,j+1) vis) = visit (i,j+1) (add (i,j+1) vis) mat cond | move_down (i,j) mat cond && not (visited (i+1,j) vis) = visit (i+1,j) (add (i+1,j) vis) mat cond | move_left (i,j) mat cond && not (visited (i,j-1) vis) = visit (i,j-1) (add (i,j-1) vis) mat cond | move_up (i,j) mat cond && not (visited (i-1,j) vis) = visit (i-1,j) (add (i-1,j) vis) mat cond | otherwise = vis

    Read the article

  • GHCi error: Not in scope

    - by dmindreader
    I'm trying to compile this function from Learn You a Haskell for Great Good. removeNonUppercase st = [ c | c <- st, c `elem` ['A'..'Z']] by placing it into a removeNonUpperCase.hs file. It compiles fine, but when passing the argument: ghci> removeNonUppercase "Hahaha! Ahahaha!" the compiler says: <interactive>:1:0: Not in scope: 'removeNonUpperCase' Why?

    Read the article

  • Prolog newbie question: Making a procedure to print Hello World

    - by dmindreader
    I want to load this simple something into my Editor: Write:-repeat,write("hi"),nl,fail. So that it prints "hi". What should I do? I'm currently trying to do File->New and Saving a file named Write into E:\Program Files\pl\xpce\prolog\lib When doing the query: ?-Write. It's printing: 1 ?- Write. % ... 1,000,000 ............ 10,000,000 years later % % >> 42 << (last release gives the question) Why?

    Read the article

  • Prolog n00b question: Doing whatever

    - by dmindreader
    I want to load this simple something into my Editor: Write:-repeat,write("hi"),nl,fail. So that it prints "hi". What should I do? I'm currently trying to do File->New and Saving a file named Write into E:\Program Files\pl\xpce\prolog\lib When doing the query: ?-Write. It's printing: 1 ?- Write. % ... 1,000,000 ............ 10,000,000 years later % % >> 42 << (last release gives the question) Why?

    Read the article

  • Understanding this Pascal-FC threaded code

    - by dmindreader
    **Program Parcial2; type buffer = channel of integer; var buffers : array [1..2] of buffer; val:integer; process sleeper (id:integer); var i : integer; begin for i:=1 to 10 do begin sleep (random(10*id)); **buffers (id):any;** end; end; process troll; begin **buffers[1]: random(10);** end;** What are buffers(id):any and buffers[1]:random(10) doing?

    Read the article

  • Understanding Haskell's filter

    - by dmindreader
    I understand that Haskell's filter is a high order function (meaning a function that takes another function as a parameter) that goes through a list checking which element fulfills certain boolean condition. I don't quite understand its definition: filter:: (a->Bool)->[a]->[a] filter p [] = [] filter p (x:y) | p x = x:filter p y | otherwise = filter p y I understand that if I pass an empty list to the function, it would just return an empty list, but how do I read the last two lines?

    Read the article

  • Understanding this matrix transposition function in Haskell

    - by dmindreader
    This matrix transposition function works, but I'm trying to understand its step by step execurtion and I don't get it. transpose:: [[a]]->[[a]] transpose ([]:_) = [] transpose x = (map head x) : transpose (map tail x) with transpose [[1,2,3],[4,5,6],[7,8,9]] it returns: [[1,4,7],[2,5,8],[3,6,9]] I don't get how the concatenation operator is working with map. It is concatenating each head of x in the same function call? How?

    Read the article

  • Uva's 3n+1 problem

    - by dmindreader
    I'm solving Uva's 3n+1 problem and I don't get why the judge is rejecting my answer. The time limit hasn't been exceeded and the all test cases I've tried have run correctly so far. import java.io.*; public class NewClass{ /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { int maxCounter= 0; int input; int lowerBound; int upperBound; int counter; int numberOfCycles; int maxCycles= 0; int lowerInt; BufferedReader consoleInput = new BufferedReader(new InputStreamReader(System.in)); String line = consoleInput.readLine(); String [] splitted = line.split(" "); lowerBound = Integer.parseInt(splitted[0]); upperBound = Integer.parseInt(splitted[1]); int [] recentlyused = new int[1000001]; if (lowerBound > upperBound ) { int h = upperBound; upperBound = lowerBound; lowerBound = h; } lowerInt = lowerBound; while (lowerBound <= upperBound) { counter = lowerBound; numberOfCycles = 0; if (recentlyused[counter] == 0) { while ( counter != 1 ) { if (recentlyused[counter] != 0) { numberOfCycles = recentlyused[counter] + numberOfCycles; counter = 1; } else { if (counter % 2 == 0) { counter = counter /2; } else { counter = 3*counter + 1; } numberOfCycles++; } } } else { numberOfCycles = recentlyused[counter] + numberOfCycles; counter = 1; } recentlyused[lowerBound] = numberOfCycles; if (numberOfCycles > maxCycles) { maxCycles = numberOfCycles; } lowerBound++; } System.out.println(lowerInt +" "+ upperBound+ " "+ (maxCycles+1)); } }

    Read the article

  • What is wrong with this SimPy installation?

    - by dmindreader
    Alright, I have tried a bunch of times the python setup.py install command from my command prompt, and this is what I'm getting: SCREEN And when trying this: from SimPy.Simulation import * on Idle, I get this: Traceback (most recent call last): File "C:/Python30/pruebas/prueba1", line 1, in <module> from SimPy.Simulation import * File "C:\Python30\SimPy\Simulation.py", line 320 print 'SimPy.Simulation %s' %__version__, ^ SyntaxError: invalid syntax >>>

    Read the article

1