0

I'd like to know how to define this as a list within Haskell so that I could perform operations such as tail, reverse and length:

cars = "lamborghinis are my favourite types of car" 

I've tried:

let cars = [lamborghinis,are,my,favourite,types,of,car] let cars = ["lamborghinis","are","my","favourite","types","of","car"] let cars = ['lamborghinis','are','my','favourite','types','of','car'] 

I have been using http://learnyouahaskell.com/starting-out as a tutorial as I am new to Haskell and I can't see where I'm going wrong, I thought my first attempt above would be correct as that it how it does it in the tutorial but with numbers instead of words.

The error I am getting is: parse error on input 'of.

Any ideas where I'm going wrong? Thanks.

1
  • 1
    Strings are already lists, i.e. String is a type synonym for [Char]. For example, "hello" and ['h','e','l','l','o'] are the same things. What is it that you want to do exactly? Commented Mar 17, 2014 at 13:26

2 Answers 2

4
let cars = "lamborghinis are my favourite types of car" 

makes cars a list of characters [Char]==String and head cars should give you the first letter. This is special syntax of haskell for strings

let cars2 = ["lamborghinis","are","my","favourite","types","of","car"] 

This is the normal form for defining lists and gives you a list of strings [String] and head cars should give you "lamborghinis". You can also split a sentance into words using the words function.

let cars3 = words cars 
Sign up to request clarification or add additional context in comments.

Comments

1

The type String is only a type synonym for [Char].

This means that for example "Hello, you" is the same as ['H','e','l','l','o',',',' ','y','o','u']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.