import Data.List
step :: [Int] -> String -> [Int]
step (x:y:ys) "*" = (x * y):ys
step (x:y:ys) "+" = (x + y):ys
step (x:y:ys) "-" = (y - x):ys
step xs numberString = read numberString:xs
This my code and error i keep getting is below, please any help is appreciated. Thank you.
*Main> step [2+6]: this is what i put into my terminal
<interactive>:5:1: error:
• No instance for (Show (String -> [Int]))
arising from a use of ‘print’
(maybe you haven't applied a function to enough arguments?)
• In a stmt of an interactive GHCi command: print it
*Main>
With step [2+6]
you are only partially applying your function, so the return value is not a list [Int]
, but a function String -> [Int]
. (Partially applying a function means to only provide some of the arguments it needs and getting a function back that takes the rest of the arguments. Yes, you can do this in Haskell!)
The error you see, will only arise in the terminal, because ghci tries to apply show
to every statement, but functions are not showable.
So if you want to fully apply your function to see it in action, you need to provide all the arguments it needs, i.e.
step [1, 2, 3] "+"
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments