haskell

What is the equivalent of this Python code in Hasekell?

What is the equivalent of this Python code in Hasekell? Question: I try to make the below Haskell code equal to the Python code, but it doesn’t work and i can’t spot the error? Can somebody spot the error? The Python output is what i want def balanceList(lst): length = len(lst) if length<3: return lst …

Total answers: 1

What exactly is meant by "partial function" in functional programming?

What exactly is meant by "partial function" in functional programming? Question: According to my understanding, partial functions are functions that we get by passing fewer parameters to a function than expected. For example, if this were directly valid in Python: >>> def add(x,y): … return x+y … >>> new_function = add(1) >>> new_function(2) 3 In …

Total answers: 3

Python equivalent of Haskell's [1..] (to index a list)

Python equivalent of Haskell's [1..] (to index a list) Question: I have a list of elements in python. I don’t know the number of elements in the list. I would like to add indexes to the list. In Haskell, I could do the following zip [1..] “abcdefghijklmnop” [(1,’a’),(2,’b’),(3,’c’),(4,’d’),(5,’e’),(6,’f’),(7,’g’),(8,’h’),(9,’i’),(10,’j’),(11,’k’),(12,’l’),(13,’m’),(14,’n’),(15,’o’),(16,’p’)] Now imagine that the string was of …

Total answers: 5

Haskell program involving `read` is much slower than an equivalent Python one

Haskell program involving `read` is much slower than an equivalent Python one Question: As part of a programming challenge, I need to read, from stdin, a sequence of space-separated integers (on a single line), and print the sum of those integers to stdout. The sequence in question can contain as many as 10,000,000 integers. I …

Total answers: 3

Floating point math in different programming languages

Floating point math in different programming languages Question: I know that floating point math can be ugly at best but I am wondering if somebody can explain the following quirk. In most of the programing languages I tested the addition of 0.4 to 0.2 gave a slight error, where as 0.4 + 0.1 + 0.1 …

Total answers: 3

Haskell equivalent of data constructors in Python?

Haskell equivalent of data constructors in Python? Question: In Haskell, I can define a binary tree as follows: data Bint a = Leaf a | Branch a (Bint a) (Bint a) then I can some operations on it as follows: height (Leaf a) = 1 height (Branch a l r) = 1 + (max (height …

Total answers: 5

Is there a Python equivalent of the Haskell 'let'

Is there a Python equivalent of the Haskell 'let' Question: Is there a Python equivalent of the Haskell ‘let’ expression that would allow me to write something like: list2 = [let (name,size)=lookup(productId) in (barcode(productId),metric(size)) for productId in list] If not, what would be the most readable alternative? Added for clarification of the let syntax: x …

Total answers: 10

Python faster than compiled Haskell?

Python faster than compiled Haskell? Question: I have a simple script written in both Python and Haskell. It reads a file with 1,000,000 newline separated integers, parses that file into a list of integers, quick sorts it and then writes it to a different file sorted. This file has the same format as the unsorted …

Total answers: 7

zipWith analogue in Python?

zipWith analogue in Python? Question: What is the analogue of Haskell’s zipWith function in Python? zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] Asked By: Yrogirg || Source Answers: You can use map: >>> x = [1,2,3,4] >>> y = [4,3,2,1] >>> map(lambda a, b: a**b, x, y) [1, 8, …

Total answers: 6

How to translate this Math Formula in Haskell or Python? (Was translated in PHP)

How to translate this Math Formula in Haskell or Python? (Was translated in PHP) Question: I’m trying to convert a Math Formula into PHP code. You can see the formula in the accepted answer here: Applying a Math Formula in a more elegant way (maybe a recursive call would do the trick). I’m not a …

Total answers: 3