haskell

how to take a specific value from a tuple list in haskell?

Hi all, I have a function like this: selectValue1 :: Int -> [(Int,Int)] -> [Int] selectValue1 a [(x,y)]= [ y |(x,y)<-[(x,y)],x<-(x,y),x==a ] what i want to do is to pass a tuple list to the function and take the second item in the tuple if the first item in the tuple matches with the input a.But this function give me an error: Type ...

Haskell ByteStrings - ending up with large file loaded into memory

Greetings, I'm trying to understand why I'm seeing the entire file loaded into memory with the following program, yet if you comment out the line below "(***)" then the program runs in constant (about 1.5M) space. EDIT: The file is about 660MB, the field in column 26 is a date string like '2009-10-01', and there are one million lines....

What's the difference between undefined in Haskell and null in Java?

Both are terms whose type is the intersection of all types (uninhabited). Both can be passed around in code without failing until one attempts to evaluate them. The only difference I can see is that in Java, there is a loophole which allows null to be evaluated for exactly one operation, which is reference equality comparison (==)--where...

Split a number into its digits with Haskell

Given an arbitrary number, how can I process each digit of the number individually? Edit I've added a basic example of the kind of thing Foo might do. For example, in C# I might do something like this: static void Main(string[] args) { int number = 1234567890; string numberAsString = number.ToString(); foreach(char x in ...

The questions regarding my first haskell program

The program return all possible combinations of '0' and '1' length N. addToElement :: String -> String -> String addToElement element symbol = element ++ symbol addOneToElement :: String -> String addOneToElement element = addToElement element "1" addZeroToElement :: String -> String addZeroToElement element = addToEle...

Saving my running toplevel for later

Hi, When working in the ocaml or ghci toplevels I often build up a significant "context" for want of a better word, values bound, functions, modules loaded, and so on. Is there a way to save all of that and reload it later so I can continue exactly where I left off? Or better yet, dump out the entire lot as a text file that could be rel...

Integer to float

This code works: posToXY :: Float -> Float -> Integer posToXY a b = do let y = a / b round y But this doesn't work: posToXY :: Integer -> Integer -> Integer posToXY a b = do let y = a / b round y I understand that operation '/' doesn't define for Integer type, but I don't know how to fix code to work...

How to use fromInteger with no implicit prelude in Haskell?

The following program yields an error in ghci: {-# LANGUAGE NoImplicitPrelude #-} import Prelude (Integer, Bool) import qualified Prelude class Discrete a where (==) :: a -> a -> Bool instance Discrete Integer where (==) = (Prelude.==) class Monoid a where one :: a (*) :: a -> a -> a fromInteger :: Integer -> a ...

Why can't I do (flip (+).digitToInt) $ '4' 4

I'm just wondering how $ works: I was expecting > (flip (+).digitToInt) $ '4' 4 <interactive>:1:24: Couldn't match expected type `t -> Char' against inferred type `Char' In the second argument of `($)', namely '4' 4 In the expression: (flip (+) . digitToInt) $ '4' 4 In the definition of `it': it = (flip (+) ...

suitable languages for a semantic web project

Currently I am thinking and searching for the adequate programming languages for a project (more than one programming language will be used). Questions: Is there a big mistake presented in my list (other than my belief of go as a suitable language)? Any tipps before I start? project: opensource project with semantic web (including rdf/...

Explicit type signatures for polymorphic types.

I'm reviewing Haskell: The Craft of Functional Programming, however the type signatures on page 356 have thrown me for a loop. Here is a simple one: succeed :: b -> Parse a b succeed val inp = [( val, inp )] How can that be b -> Parse a b, if succeed Int -> Int succeed a = a and, succeed Int -> Int -> Int succeed a b = a + b Th...

Haskell: can't use getCPUTime

Hi I have: main :: IO () main = do iniciofibonaccimap <- getCPUTime let fibonaccimap = map fib listaVintesete fimfibonaccimap <- getCPUTime let difffibonaccimap = (fromIntegral (fimfibonaccimap - iniciofibonaccimap)) / (10^12) printf "Computation time fibonaccimap: %0.3f sec\n" (difffibonaccimap :: Double) lis...

Is this Fibonacci sequence function recursive?

Consider the following (Haskell) code: fib=0:1:zipWith (+) fib (tail fib) A coworker is trying to assert that this is not a recursive function because fib is simply a list that defines itself with itself and that is somehow different than a function that does the same. I think he's smoking crack. What do you think? ...

Natural number sequence generator

As i understand Haskell does not have a global state, so is there any way to write a function f that will return f(n - 1) + 1, where n is a number of function call and f(1) = 0. It should not accept any arguments and used like func f Prelude> f () 0 Prelude> f () 1 ...

Find out whether all given files exists in haskell

I'm a newbie, and the monads get me totally confused. Given a list of filenames i'd like to know whether all the files exist. Generally, i'd like to do: import System.Directory allFilesPresent files = foldr (&&) True (map doesFileExist files) However i do not know what's the right way to do it, because there's IO Bool instead of Bool...

Haskell programing: map and fold on search trees

I have a search tree thats defined as data (Ord a) => Stree a = Null | Fork(Stree a) a (Stree a) deriving Show and I have to define two functions, mapStree: mapStree :: (Ord b, Ord a) => (a -> b) -> Stree a -> Stree b and foldStree: foldStree :: (Ord a) => (b -> a -> b -> b) -> b -> Stree a -> b I dont fully understand whats g...

Use 'let' in 'if' expression

I need a function that works like this: foo :: Integer -> Integer -> [Integer] foo a b = do let result = [] let Coord x y = boo a b if x > 0 let result = result ++ [3] if y > 0 let result = result ++ [5] if x < a let result = result ++ [7] ...

Haskell Network.Browser HTTPS Connection

Is there a way to make https calls with the Network.Browser package. I'm not seeing it in the documentation on Hackage. If there isn't a way to do it with browse is there another way to fetch https pages? My current test code is import Network.HTTP import Network.URI (parseURI) import Network.HTTP.Proxy import Data.Maybe (fromJust) imp...

how to use Cabal?

how to use Cabal? ...

Lazy Evaluation?

Hello What is wrong here, is Lazy Evaluation too? teste.hs module Main where import Control.Parallel(par,pseq) import Text.Printf import Control.Exception import System.CPUTime import Data.List import IO import Data.Char import Control.DeepSeq --Calcula o tempo entre o inicio e o fim de rodagem do programa ti...