Tagged Questions
2
votes
0answers
35 views
Functional way in IO related tasks
Most beginners program IO related tasks in an imperative way. Are there any general suggestions to help beginners switching to more functional way in implementing IO related tasks?
As a concrete ...
3
votes
3answers
114 views
Checking if a password is strong enough - Haskell vs procedural language
I implemented a function in Python that checks if a password is strong enough. A password is strong enough if it passes 3 out of 5 checks. This is the Python function:
def is_valid(password):
...
1
vote
1answer
57 views
How to implement a deep-tree-set algorithm using fold?
This is the pseudocode for a function that will modify a specific node on a tree given a path and a new value:
path_set val path tree =
| empty? path -> val
| otherwise -> set ...
1
vote
1answer
75 views
GHC won't run this function, but does compile it
This is the code:
finde_f x =
if (x-2) mod 3 /= 0
then 1
else x - (x-2)/3
These are the errors during run-time:
*Main> finde_f 6
<interactive>:170:1:
No instance for ...
0
votes
2answers
84 views
Haskell function example, why infinite list doesnt stop
Why doesnt map sqrt[1..] not give an infinite recursion????
How can i better understand the haskell?
sqrtSums :: Int
sqrtSums = length ( takeWhile (<1000) (scanl1 (+) (map sqrt[1..]))) + 1
2
votes
2answers
127 views
Does OCaml have fusion laws
Recently I am reading some functional programming books involving Haskell.
It seems Haskell quite fancies “modular programs”, for example,
f : (Integer;Integer) ! Integer
f = sum . map . sq . ...
3
votes
3answers
73 views
Calculating types of Haskell Functions
Having troubles with manually calculating types of given functions in Haskell for an exam at the weekend.
I understand the basics such as:
i x = x :: t -> t
k x y = x :: t -> t1 -> t
But having ...
1
vote
1answer
62 views
Displaying the stemming word and the stemming using haskell
hi i am new to Haskell and functional programing.
i want to find the stemming word in the string and display the word and the word after the stemming is removed.
eg if the string is : "he is a good ...
3
votes
1answer
115 views
Lambda Calculus (λa.b)((λx.xx)(λx.xx)) [on hold]
Im looking for an example of a weakly normalising lambda term.
Am I right in saying that the following:
(λa.b)((λx.xx)(λx.xx))
Reduces to:
b
or:
doesnt terminate (if you try to reduce ...
1
vote
1answer
76 views
Can the same fusion law for foldr be applied to foldlmap?
I've read there is no fusion law for foldl alone. If I guess correctly, this is due to foldl awkwardness for implementing map - which is mostly due to (foldl cons nil xs) reversing the list.
If ...
5
votes
2answers
119 views
Is it possible to implement foldl/foldr using unsided fold?
By unsided fold, I mean a hypothetic primitive fold operation for associative operators that, does not guarantee any ordering. That is, (fold + 0 [a b c d]) could be (+ (+ a b) (+ c d)) or (+ (+ (+ a ...
-1
votes
2answers
143 views
Cannot IO in haskell
I have a function to count no of times each word is repeated in a string:
keywords :: String -> [String]
keywords = words . map (\x -> if isAlpha x then x else ' ')
count :: Ord a => [a] ...
9
votes
2answers
123 views
Fundamentals of Haskell Type Classes and “could not deduce (~) from the context (~)” error
I'm relatively new to Haskell, and I believe I'm misunderstanding something fundamental about type classes. Suppose I'd like to create a type class 'T' implementing n-ary trees supported by four ...
2
votes
1answer
77 views
counting no of times each word is repeated using haskell
iam new to Haskell and functional programing...
i want to pass in the function as a string and count no of times each syntax such as (if,else, elseif for, while, dowhile) is exist in the function ...
4
votes
2answers
100 views
What are the properties of the unsided fold?
Foldl and folr are 2 very important functions for FP and Haskell, but I have never heard much about the unsided fold:
fold f [a,b,c,d] = (f (f a b) (f c d))
That is, a fold that operates on binary ...
14
votes
2answers
176 views
How exactly does Stream Fusion work?
The only resources on Stream Fusion I can find are papers introducing it, which aren't really the best learning sources. How exactly does stream fusion work?
More specifially, as this is the part ...
1
vote
1answer
128 views
Which compilers today are capable of converting (map f (map g h)) into (map (f . g) h)? [closed]
Many techniques are used for this means, from as simple as Short Cut Fusion to elaborate Stream Fusion. I'm aware compilers such as GHC and MLTon rely considerably on this technique. Are there other ...
6
votes
5answers
237 views
+100
Can compilers deduce/prove mathematically?
I'm starting to learn functional programming language like Haskell, ML and most of the exercises will show off things like:
foldr (+) 0 [ 1 ..10]
which is equivalent to
sum = 0
for( i ...
0
votes
3answers
59 views
removing the repeated words using haskell
i want to remove the repeated words in the string..
eg: if input is "he is the the good boy boy"
out put is "he is the good boy"
i done the coding but it is not working properly.
...
-2
votes
1answer
53 views
Postal address validation using haskell
i am extremely new to Haskell..
i want to validate postal address using Haskell.
29b, roadname, cityname, postalcode, country
i want to validate the following
1)first section (29b)must have at ...
0
votes
3answers
91 views
removing the stemming words using haskell
i am new to haskell and functional programing..
my aim is to remove the stemming words from the given string..
eg: input is : "he is fishing and catched two fish"
output is : "he is fish and ...
4
votes
2answers
139 views
Is there an indexed list in Haskell and is it good or bad?
I am a new comer to the Haskell world and I am wondering if there is something like this:
data IndexedList a = IList Int [a]
findIndex::(Int->Int)->IndexedList a->(a,IndexedList a)
...
0
votes
1answer
87 views
stemming words removal in haskell
am new to Haskell and functional programing..
my aim is to get a string from user and check whether there is any stemming words available in that string. if so i want to remove the stemming and ...
0
votes
1answer
66 views
having error when trying to calculating number of words per paragraph in Haskell
I am new to Haskell and functional programing.
My aim is to calculate the number of paragraphs in a text document and the number of words in each paragraph.
I try to do this by the following steps:
...
4
votes
2answers
131 views
understanding the type signature of (.)
I'm just learning Haskell and functional programming using Richard Bird's book and came across the type signature of the (.) function. Namely
(.) :: (b -> c) -> (a -> b) -> (a -> c)
...
1
vote
1answer
106 views
How to make multiple tests and get multiple results all toghether for a variable in one function?
i am working with a game board, that maps pieces by coordinates. For this example consider the piece "B" with orientation "N" on the coordinates (0,0). My coordinates are displayed in a pair of ...
1
vote
2answers
82 views
function parameters in haskell
I can use parenthesis to make cons operator have more priority than function application in this patter match equation:
tail (_:xs) = xs
However it will be "parse error in pattern" if I try to ...
2
votes
1answer
60 views
Constraint: Two type variables can be used with an operator
I have the following function that compiles and runs successfully:
-- Linearly interpolates between two values. That is, it is a function which:
-- returns y1 when x = x1
-- returns y2 when x = ...
9
votes
4answers
244 views
How can non-determinism be modeled with a List monad?
Can anyone explain (better with an example in plain English) what a list monad can do to model non-deterministic calculations? Namely what the problem is and what solution a list monad can offer.
3
votes
2answers
82 views
Unit testing several implementations of a functional data structure without code duplication
As part of an assignment on functional data types, we're asked to give different implementations of queues in Haskell, two of which are given below.
Coming from an OO world, the first reflex is to ...
11
votes
2answers
429 views
How are J/K/APL classified in terms of common paradigms?
I've just started learning J, which is very interesting, but I was wondering what kind of language it is exactly, in relation to common paradigms and classifications. For example, you could say that ...
4
votes
2answers
341 views
Haskell converting list of points into string
I am working on the assignment and I need some help. This is the last part but I am really struggling with it, don't know how to approach. Here's the problem:
Add a function render which takes an ...
2
votes
3answers
158 views
Functionally solving questions: how to use Haskell?
I am trying to solve one of the problem in H99:
Split a list into two parts; the length of the first part is given.
Do not use any predefined predicates.
Example:
> (split '(a b c d e f g h i k) ...
2
votes
2answers
146 views
First attempt at Haskell: Converting lower case letters to upper case
I have recently started learning Haskell, and I've tried creating a function in order to convert a lower case word to an upper case word, it works, but I don't know how good it is and I have some ...
3
votes
1answer
89 views
Convert a Haskell function to SML
I'm trying to convert a Haskell function, which displays a boolean formula, to a SML function.
The function:
data Formula
= Atom String
| Neg Formula
| Conj Formula Formula
| Disj ...
2
votes
1answer
94 views
Better display of boolean formulas
I want to implement a method for showing a propositional formula in SML. The solutions that I found so far was of this type:
fun show (Atom a) = a
| show (Neg p) = "(~ " ^ show p ^ ")"
| show ...
5
votes
2answers
220 views
Haskell for Lambda Calculus, Type Inferencing
My adventure in Haskell programming hasn't been all epic. I am implementing Simple Lambda Calculus, and I am glad to have finished Syntax, Evaluation, as well as Substitution, hoping they are correct. ...
3
votes
1answer
86 views
Haskell generics with unique ids
I've been using Uniplate for a while, but I find the lack ability to identify a node to make things more difficult for me. Is there a generics implementation that allows absolute ids? Here is an ...
1
vote
3answers
79 views
Haskell Netwire - Type errors
I have just started using netwire and I'm having trouble with the very basics.
The following code works fine for me:
main :: IO ()
main = testWire clockSession_ (for 3 . yeah)
yeah :: Monad m => ...
3
votes
5answers
138 views
What is a pure functional approach to indexing functions?
For example, a function that receives a list of trades and returns a list of value sums, indexed by time:
trades = [{time:1,value:8}, {time:1.1,value:8},... {time:1.2,value:7}, time:2.1,value:8} ...]
...
4
votes
2answers
133 views
Non tail-recursive function not blowing up in GHCi. Why?
I was expecting to see my stack blow with the following code.. yet it didn't:
*Main> let blowwss x = if x == 0 then 0 else (1 + blowwss (x-1))
*Main> blowwss 1000000
1000000
The function ...
2
votes
3answers
85 views
Is an expression in a list comprehension evaluated redundantly?
let strings = ["foo", "bar", "baz"]
in [filter (== char) (concat strings) | char <- "oa"]
Does GHC evaluate concat strings when char == 'o' and then again when char == 'a'? Or does it remember ...
2
votes
2answers
96 views
Is it possible to create a Functor instance for sorted binary trees in Haskell?
Imagine we have a SortBinTree type constructor defined as, for example,
data SortBinTree a = EmptyNode | Node a (SortBinTree a) (SortBinTree a);
It make sense only when a is an instance of Ord type ...
4
votes
4answers
116 views
Less redundant way to make instances of a “circular” enum typeclass
I have a snippet of code in which I have declared two datatypes. I've auto-derived both as members of the typeclass enum, however, I dislike that they are not "circular". By this, I mean that calling ...
1
vote
2answers
155 views
How do you write foldWhile with the standard library?
is it possible to write the folowing code with the scala standard library ?
def foldWhile[A,B](s: Stream[A])(z: B)(w: B ⇒ Boolean)(op: (B, A) ⇒ B): B = {
if(s.isEmpty) z
else {
...
1
vote
2answers
85 views
If functions as instances of the Show typeclass
Functions aren't instances of the Show typeclass, so one cannot see it in neat form. But compiler has it somewhere on which it returns fails.
So why it is not possible to show it and if it can be ...
-1
votes
1answer
40 views
How can i fix the haskell hugs error Instance of Integral Bool required for definition of
I am writing a haskell module for hugs and i get the error shown in the title.
What does this mean and how do i fix this?
the code in question is the second line
and1 :: [Bool] -> Bool
...
1
vote
3answers
325 views
Object oriented programming in Haskell
I'm trying to get an understanding of object oriented style programming in Haskell, knowing that things are going to be a bit different due to lack of mutability. I've played around with type classes, ...
2
votes
2answers
110 views
What is a polymorphic lambda?
The concept of lambdas (anonymous functions) is very clear to me. And I'm aware of polymorphism in terms of classes, with runtime/dynamic dispatch used to call the appropriate method based on the ...
1
vote
3answers
83 views
Haskell IO and arrays with recursion
I am having trouble writing a function with type
Int -> IO [Int] or Int -> [IO Int]
I have the following code that is not working:
createEIList :: Int -> IO [Int]
createEIList len = do
...