I just learned from another question that Haskell is called a curried programming language because it applies function currying by default. What are other languages that display this behavior?
Of the less esoteric languages it is mainly Haskell:
f x y z = x + y * z
g = f 4
r = g 7 8
OCaml and F#:
let f x y z = x + y * z
let g = f 4
let r = g 7 8
and to a lesser extent SML (where libraries use currying less):
fun f x y z = x + y * z
val g = f 4
val r = g 7 8
Related
I'm studying for a syntax exam in Haskell and would love to receive some help of why this equals to 2:
" Consider the function:
f x = let f x = x+1 in f (f x)
What is the value of let x=0 in f x ? "
The answer to this is 2.
But "let f x = x+1" means that f (x+1). And then if we let x = 0 wouldn't that leave f(1)?
Thank you for reading
There are two different functions being used. In the original function, there is no recursion; all references to f refer to the function defined by the let expression, not the function being defined. There are also two different scopes both containing a variable named x. It's clearer if you rename the inner function and its argument:
f x = let f' x' = x' + 1 in f' (f' x)
Now the other expression is easier to evaluate using equational reasoning:
let x = 0 in f x == f 0
== let f' x' = x' + 1 in f' (f' 0)
== (f' 0) + 1
== (0 + 1) + 1
== 1 + 1
== 2
I am trying to achieve the following (defined in Javascript) in Haskell:
JS:
const fold = (c, h) => {
const f = (n) => {
return n === 0 ? c : h (f(n-1))
}
return f
}
fold(1, (x)=>x*10)(3)
Repl Link: https://repl.it/repls/ExperiencedTeemingLorikeet
I tried something along these lines (but does not work):
foldn c h =
f = f' n
where f' 0 = c
f' n = h(f'(n-1))
f
Essentially I am trying to create a named curried function "f" that can be returned. Also note that the definition of "f" is recursive
How can I do this in Haskell?
Actually, all functions in Haskell are curried by default, so you can just do
fold c h n = if n == 0 then c else h (fold c h (n-1))
If you still prefer to have an f inside, then
fold c h = f
where
f 0 = c
f n = h (f (n-1))
These versions are equivalent, and can be called like
fold 1 (*10) 3
You're close. Just declare a function that closes over c and h.
foldn c h = f where
f 0 = c
f n = h . f $ n-1
So I've only a little experience with Haskell and I've been working on the below program to implement a search to find maxima in a function, yet I've been receiving an odd error. When I compile it says:
MaximaSearch.hs:26:1:
parse error (possibly incorrect indentation or mismatched brackets)
That's the line that says "main = do" so I think it's some sort of trailing error from my indentation in the code preceding it but I can't spot any mistakes...
Here's the code:
module Main where
g :: Float -> Float
--your function here
g x = cos(x^2)
--goldenSectionSearch
goldenSS :: (Float -> Float) -> Float -> Float -> Float -> Float -> Float
goldenSS f a b c tau
| (c-a) < tau * (abs b + abs x) = (c+a)/2
|f x > f b = let
t1|(c - b) > (b-a) = goldenSS f b x c tau
|otherwise = goldenSS f a x b tau
in t1
|otherwise = let
t2|(c-b) > (b-a) = goldenSS f a b x tau
|otherwise = goldenSS f x b c tau
in t2
where
let x
| (c-b) > (b-a) = b + resphi*(c-b)
|otherwise = b - resphi*(b-a)
where resphi = 2 - phi where phi = (1+ sqrt 5)/2
in x
--main
main = do
print x
print (g x)
where
x = goldenSS g a ((a+b)/2) b tau
where
a = 2
b = 3
tau = 0.001
any ideas?
The reason you're getting the parse error stems from the non-idiomatic usage of let and where bindings in your code.
Haskell allows multiple syntactic structures for temporary bindings and pattern matching, but you're combining them in a rather strange and messy manner.
To learn how to write the code cleaner and in a manner more usual for Haskell, I'd recommend looking up existing haskell libraries and programs (for example on hackage) to get a feel for how let and where bindings usually work. In general I find that for pure functions I almost exclusively use where (as opposed to let), but certain things are stylistic.
As for this code, I modified it a bit to use where bindings instead of let, and it compiles and runs for me now. Even if you have to tweak it a bit to get it to compile for you, this overall structure is cleaner and less likely to give you parse errors:
module Main where
g :: Float -> Float
g x = cos(x^2)
goldenSS :: (Float -> Float) -> Float -> Float -> Float -> Float -> Float
goldenSS f a b c tau
|(c-a) < tau * (abs b + abs x) = (c+a)/2
|f x > f b = t1
|otherwise = t2
where x | (c-b) > (b-a) = b + resphi*(c-b)
|otherwise = b - resphi*(b-a)
resphi = 2 - phi
phi = (1+ sqrt 5)/2
t1 |(c - b) > (b-a) = goldenSS f b x c tau
|otherwise = goldenSS f a x b tau
t2 |(c-b) > (b-a) = goldenSS f a b x tau
|otherwise = goldenSS f x b c tau
main =
do
print x
print (g x)
where x = goldenSS g a ((a+b)/2) b tau
a = 2
b = 3
tau = 0.001
Consider the following definition of Rose Trees:
data RTree a = R a [RTree a]
I need help defining the function rtHeight :: (RTree a) -> Int that calculates the height of a rose tree.
So far, I have tried the following
rtHeight R a [] = 1
rtHeight R a l = 1 + rtHeight l
However, this does not work becuase l is a list of rose trees.
I have also tried the following:
rtHeight R a [] = 1
rtHeight R a l = maximum (map (rtHeight) l)
I believe this fails becuase I am not adding a level while I am going down the tree.
Here is my final answer. Tested and it worked:
rtHeight R a [] = 1
rtHeight R a l = 1 + maximum (map (rtHeight) l)
In Why Functional Programming Matters (PDF), the author includes a code that is equivalent to the following:
reduce_tree f g z t =
f (label t) (reduce (g . reduce_tree f g z) z (branches t))
Using it, we can write
rtHeight t = reduce_tree f g z t
where
f _ y = 1 + y -- 1 more than maximum height of subtrees
g x y = max x y -- y is maximum height of subtrees to the right
z = 0 -- the smallest height is 0
label (R a _ ) = a
branches (R _ bs) = bs
reduce = foldr
As an illustration, for a tree t = R a [b,c,d], this calculates t's height as
rtHeight t = 1 + max (rtHeight b) -- rtHeight == reduce_tree f g z
(max (rtHeight c)
(max (rtHeight d)
0))
That is because, for the built-in foldr function,
foldr g z [a,b,c,...,n] == g a (g b (g c (... (g n z)...)))
An interesting identity is foldr (g . h) z xs == foldr g z (map h xs), and since maximum (xs ++ [0]) == foldr max 0 xs, your direct recursive formulation of rtHeight can be recovered from this generalized formulation.
Lets say I have the following:
f :: a -> b -> c
g :: b -> c
g = f 10
Now lets say f is actually:
f x y = f1 x + y
Would:
g `seq` ...
actually evaluate f1 10, so later when running
g 9
it's actually just a simple addition?
If not, is there a way to "evaluate" parts of a partially applied function?
I'm looking for a generic solution, one that doesn't depend on knowing how f and g work.
No, it will not, because in general, the choice of right hand side for f might depend on y. If you want to share the result of f1 x between calls to g, you would have to write f like this:
f x = let z = f1 x in \y -> z + y
Of course, due to laziness this will not evaluate f1 x until the first time g is called. To have g `seq` ... force evaluation of f1 x, you would have to write:
f x = let z = f1 x in z `seq` (\y -> z + y)
seq is shallow:
Prelude> let f1 = undefined
Prelude> let f = \x -> \y -> f1 x + y
Prelude> let g = f 10
Prelude> g `seq` 1
1
Prelude> g 9
*** Exception: Prelude.undefined
Prelude>
I'd take a look at Control.DeepSeq: http://hackage.haskell.org/packages/archive/deepseq/1.2.0.1/doc/html/Control-DeepSeq.html