This question already has an answer here:
cis.upenn.edu cis194 Haskell Basics: Functions and Pictures
(1 answer)
Closed 3 years ago.
I encountered the following Haskell code:
trafficController :: Double -> Picture
trafficController t
| round (t/3) `mod` 2 == 0 = trafficLight True
| otherwise = trafficLight False
main :: IO ()
main = animationOf trafficController
I ran and it worked. But how can it be coz the 't' parameter was never applied ?
The t parameter is indeed never applied explicitly. However, you are correct in saying that trafficController needs t to be applied in order to work. What gives?
In this case, the animationOf function is the key. Let’s have a look at its type:
animationOf :: (Double -> Picture) -> IO ()
We can see that animationOf takes a function as its first parameter. Presumably, the implementation of animationOf takes this function, figures out the correct Double to use, applies this value to the function, and then draws the resulting Picture on the screen. (Since this is an animation, it probably does this multiple times.) So even though you never apply the parameter explicitly, animationOf does it for you.
Related
As the title states, I see pieces of code online where the variables/functions have ' next to it, what does this do/mean?
ex:
function :: [a] -> [a]
function ...
function' :: ....
The notation comes from mathematics. It is read x prime. In pretty much any math manual you can find something like let x be a number and x' be the projection of ... (math stuff).
Why not using another convention? well, in mathematics It makes a lot of sense because It can be very pedagogical... In programming we aren't used to this convention so I don't see the point of using it, but I am not against it neither.
Just to give you an example of its use in mathematics so you can understand why It is used in Haskell. Below, the same triangle concept but one using prime convention and other not using it. It is pretty clear in the first picture that pairs (A, A'), (B, B'), ... are related by one being the vertex and the prime version being the midpoint of the oposite edge. Whereas in the second example, you just have to remember that A is the midpoint of the oposite edge of vertex P. First is easier and more pedagogical:
As the other answers said, function' is just another variable name. So,
don'tUse :: Int -> IO ()
don'tUse won'tBe''used'' = return ()
is just like
dontUse :: Int -> IO ()
dontUse wontBeUsed = return ()
with slightly different names. The only requirement is that the name starts with a lowercase-letter or underscore, after that you can have as many single-quote characters as you want.
Prelude> let _' = 1
Prelude> let _'' = 2
Prelude> let _''''''''' = 9
Prelude> _' + _'' * _'''''''''
19
...Of course it's not necessarily a good idea to name variables like that; normally such prime-names are used when making a slightly different version of an already named thing. For example, foldl and foldl' are functions with the same signature that do essentially the same thing, only with different strictness (which often affects performance memory usage and whether infinite inputs are allowed, but not the actual results).
That said, to the question
Haskell what does the ' symbol do?
– the ' symbol does in fact do various other things as well, but only when it appears not as a non-leading character in a name.
'a' is a character literal.
'Foo is a constructor used on the type level. See DataKinds.
'bar and ''Baz are quoted names. See TemplateHaskell.
This question already has an answer here:
How to use variable from do block assignment line in a where clause?
(1 answer)
Closed 2 years ago.
I'm trying to use the variable defined in the outer scope within an action defined in do block using where:
module Main where
main :: IO ()
main = do
outerVar <- return "a"
doSomething
where
doSomething :: IO String
doSomething = putStrLn ("outerVar: " ++ outerVar)
Given this snippet, I'm trying to understand why does the compiler return the following error:
error: Variable not in scope: outerVar :: [Char]
|
9 | doSomething = putStrLn ("outerVar: " ++ outerVar)
|
Based on my understanding - doSomething function should create some kind of a "closure" to contain the value of outerVar (which I've just found out is called a free variable), but that doesn't happen.
I've spent quite a lot of time trying to figure out why exactly does this error happen. I'm quite embarassed as, even for me as a Haskell newbie, it seems almost like a basic concept that should be obvious but it isn't - hopefully I'm wrong. I couldn't find any answers when searching for "where in do block scope", "where closure" or similar keywords. This page that compares let and where doesn't mention my case, the closest example I've found is the first bit of code from here where the "Lambda lifting" topic is covered. They're able to use n variable from outer scope within the function defined in where, but it's not in a do block as my case is.
So my question is - why exactly is outerVar variable not in scope of doSomething?
do blocks are just syntactic sugar for chaining monadic values using >>= and lambda functions. Your block is first translated by the compiler into:
return "a" >>= \outerVar -> doSomething
where doSomething =...
It should be obvious now that outerVar is not in scope outside the lambda to which it is an argument.
I'm currently working on an assignment. I have a function called gamaTipo that converts the values of a tuple into a data type previously defined by my professor.
The problem is: in order for gamaTipo to work, it needs to receive some preceding element. gamaTipo is defined like this: gamaTipo :: Peca -> (Int,Int) -> Peca where Peca is the data type defined by my professor.
What I need to do is to create a funcion that takes a list of tuples and converts it into Peca data type. The part that im strugling with is taking the preceding element of the list. i.e : let's say we have a list [(1,2),(3,4)] where the first element of the list (1,2) always corresponds to Dirt Ramp (data type defined by professor). I have to create a function convert :: [(Int,Int)] -> [Peca] where in order to calculate the element (3,4) i need to first translate (1,2) into Peca, and use it as the previous element to translate (3,4)
Here's what I've tried so far:
updateTuple :: [(Int,Int)] -> [Peca]
updateTuple [] = []
updateTuple ((x,y):xs) = let previous = Dirt Ramp
in (gamaTipo previous (x,y)): updateTuple xs
Although I get no error messages with this code, the expected output isn't correct. I'm also sorry if it's not easy to understand what I'm asking, English isn't my native tongue and it's hard to express my self. Thank you in advance! :)
If I understand correctly, your program needs to have a basic structure something like this:
updateTuple :: [(Int, Int)] -> [Peca]
updateTuple = go initialValue
where
go prev (xy:xys) =
let next = getNextValue prev xy
in prev : (go next xys)
go prev [] = prev
Basically, what’s happening here is:
updateTuple is defined in terms of a helper function go. (Note that ‘helper function’ isn’t standard terminology, it’s just what I’ve decided to call it).
go has an extra argument, which is used to store the previous value.
The implementation of go can then make use of the previous value.
When go recurses, the recursive call can then pass the newly-calculated value as the new ‘previous value’.
This is a reasonably common pattern in Haskell: if a recursive function requires an extra argument, then a new function (often named go) can be defined which has that extra argument. Then the original function can be defined in terms of go.
I'm beginning my first exploration of functional programming with Haskell. I'm struggling a bit with printing a hardcoded value. I created a model representing a car and the gears it can go to. What I want to do is simply print my hardcoded civicNinetyOne when I call printCar. But I keep getting an error when I load the file. I posted the error below, it's an indentation error but from what I've read here at LearnYouAHaskell that call function declaration is correct. Can someone point me to the cause of this issue? Thank you
Error
first_program.hs:10:1: error:
parse error (possibly incorrect indentation or mismatched brackets)
|
10 | printCar:: () -> Car | ^
Code
data Car = Car{
gears :: [String],
currentGear :: String,
brokeGears :: [String],
shiftStroke:: Strokes
}
let civicNinetyOne = Car ["gear1", "gear2", "gear3", "gear4", "gear5"] "gear1" ["gear4"] [("gear1","pull", "gear2"), ("gear2","push", "gear3"), ("gear3","skipShift", "gear5")]
printCar:: () -> Car
printCar = civicNinetyOne
printCar takes an argument like any other function; it's argument type is (), which means there is only one value (also spelled ()) that can be used to call it.
civicNinetyOne, on the other hand, is a value with type Car, not a function of type () -> Car, so cannot itself be assigned to printCar.
The correct definition is
printCar :: () -> Car
printCar () = civicNinetyOne
and it would be called as
> printCar ()
Car {gears = ["gear1","gear2","gear3","gear4","gear5"], currentGear = "gear1", brokeGears = ["gear4"], shiftStroke = [("gear1","pull","gear"),("gear2","push","gear3"),("gear3","skipShift","gear5")]}
For completeness, the let (as pointed out in the comments) is optional in recent versions of GHCi and required in older versions of GHCi, but it is forbidden in a module. (The interactive interpreter behaves somewhat like an implied do block.)
I have a recursive function working within the scope of strictly defined interface, so I can't change the function signatures.
The code compiles fine, and even runs fines without error. My problem is that it's a large result set, so it's very hard to test if there is a semantic error.
My primary question is: In a sequence of function calls A to B to A to B to breaking condition, considering the same original Map is passed to all functions until breaking condition, and that some functions only return an Integer, would an insert on a Map in a function that only returns an Integer still be reflected once control is returned to the first function?
primaryFunc :: SuperType -> MyMap -> (Integer, MyMap)
primaryFunc (SubType1 a) mapInstance = do
let returnInt = func1 a mapInstance
(returnInt, mapInstance)
primaryFunc (SubType2 c) mapInstance = do
let returnInt = primaryFunc_nonprefix_SuperType c mapInstance
let returnSuperType = (Const returnInt)
let returnTable = H.insert c returnSuperType mapInstance
(returnInt, returnTable)
primaryFunc (ConstSubType d) mapInstance = do
let returnInt = d
(returnInt, mapInstance)
func1 :: SubType1 -> MyMap -> Integer
func1 oe vt = do
--do stuff with input and map data to get return int
returnInt = primaryFunc
returnInt
func2 :: SubType2 -> MyMap -> Integer
func2 pe vt = do
--do stuff with input and map data to get return int
returnInt = primaryFunc
returnInt
Your question is almost impossibly dense and ambiguous, but it should be possible to answer what you term your "primary" question from the simplest first principles of Haskell:
No Haskell function updates a value (e.g. a map). At most it can return a modified copy of its input.
Outside of the IO monad, no function can have side effects. No function can affect the value of any variable assigned before it was called; all it can do is return a value.
So if you pass a map as a parameter to a function, nothing the function does can alter your existing reference to that value. If you want an updated value, you can only get that from the output of a function to which you have passed the original value as input. New value, new reference.
Because of this, you should have absolute clarity at any depth within your web of functions about which value you are working with. Knowing this, you should be able to answer your own question. Frankly, this is such a fundamental characteristic of Haskell that I am perplexed that you even need to ask.
If a function only returns an integer, then any operations you perform on any values made available to the function can only affect the output - that is, the integer value returned. Nothing done within the function can affect anything else (short of causing the whole program to crash).
So if function A has a reference to a map and it passes this value to function B which returns an int, nothing function B does can affect A's copy of the map. If function B were allowed to secretly alter A's copy of the map, that would be a side effect. Side effects are not allowed.
You need to understand that Haskell does not have variables as you understand them. It has immutable values, references to immutable values and functions (which take inputs and return new outputs). Functions do not have variables which are in scope for other functions which might alter those variables on the fly. That cannot happen.
As an aside, not only does the code you posted show that you do not understand the basics of Haskell syntax, the question you asked shows that you haven't understood the primary characteristics of Haskell as a language. Not only are these fundamentals things which can be understood before having learned any syntax, they are things you need to know to make sense of the syntax.
If you have a deadline, meet it using a tool you do understand. Then go learn Haskell properly.
In addition, you will find that
an insert on a Map in a function that only returns an Integer
is nearly impossible to express. Yes, you can technically do it like in
insert k v map `seq` 42 -- force an insert and throw away the result
but if you think that, for example:
let done = insert k v map in 42
does anything with the map, you're probably wrong.
In no case, however, is the original map altered.