简体   繁体   中英

Haskell Trace Function

I am on a mac and I am running Haskell through the command line. This is my code I am recursively reversing a list.

import Debug.Trace
reverse1 :: (Show a) => [a] -> [a]  
reverse1 [] = []  
reverse1 (x:xs) = trace(“input xs: “ ++ show xs) $ reverse1 xs ++ [x]

The assignment is to do it recursively and also show the trace. It does the reverse if I remove the trace information once I add it, it breaks. This is the error I receive.

[1 of 1] Compiling Main             ( reverse.hs, interpreted )

reverse.hs:4:24: lexical error at character '\8220'
Failed, modules loaded: none. 

You have a copy and paste problem: your code snippet uses fancy quotes ( ) instead of normal ones ( " ) and Haskell doesn't know how to parse it.

In the error message, "lexical error" means that there is a problem with your syntax; specifically, it doesn't know what to do with the character '\\8220' which is the ASCII representation of .

Assuming your console properly supports Unicode, you can see what character an escape code like this corresponds to with putStrLn :

Prelude GHC.Exts> putStrLn "\8220"
“

This might help you understand similar error messages in the future.

If you're in Emacs, another option is to use the command Cx 8 <RET> which allows you to input a Unicode character by number ( 8220 in this case). Unfortunately, Emacs expects a number in hexadecimal and Haskell provides one in base-10, so you have to be explicit about your radix:

C-x 8 <RET> #10r8220
“

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM